当前位置: 首页 > 知识库问答 >
问题:

React-无法读取未定义的属性“set state”

祁嘉瑞
2023-03-14

出于某种原因,我得到了错误“react-nable read属性'set state'的undefined”。因此,this.state永远不会用用户输入的值更新。当我尝试注释掉的绑定时,我会出现奇怪的行为,无法输入用户名,也不再得到null错误,但值只是未定义。如有任何帮助,不胜感激。谢了。

import __fetch from "isomorphic-fetch";
import React from "react";
import InlineCss from "react-inline-css";
import Transmit from "react-transmit";

import Header from './components/header'

class Registration extends React.Component {

    componentWillMount () {
        if (__SERVER__) {
            console.log("Hello server from Registration");
        }

        if (__CLIENT__) {
            console.log("Hello Registration screen");
        }
    }

    constructor () {
        super()

        this.state = {
            name: '',
            password: ''
        }

         //this.onChangeName = this.onChangeName.bind(this);
         //this.onChangePassword = this.onChangePassword.bind(this);
    }

    onChangeName(e) {
        //this.state.name = e.target.name
        this.setState({ name: e.target.name});
    }

    onChangePassword(e) {
        //this.state.password = e.target.name
        this.setState({ password: e.target.password });
    }


    emptinessChecker(){
        let {name, password} = this.state
        console.log(name)
        if (name === "" || password === "") {
            return true;
        }
        else {
            return false;
        }
    }

    submit() {
        console.log(this)
        console.log(this.state)
        if (this.emptinessChecker()){
            alert("Please do not leave any fields blank!");
        }
        else{
            var xhr = new XMLHttpRequest();   // new HttpRequest instance 
            xhr.open("POST", "/edit");
            xhr.addEventListener("load", e => {
                console.log(xhr.responseText)
            });

            xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
            xhr.send(JSON.stringify(this.state));
            window.location.href= "/success"
        }
    }


    render () {

        //let {username, password} = this.state

        if (__SERVER__) {
            console.log("render server from registration");
        }
        if (__CLIENT__) {
            console.log('we client now!')
        }

        return (
            <InlineCss stylesheet={Registration.css()} namespace="Registration">
                <Header />



                <div>
                    <div className = "Register" >
                        Register
                    </div>


                    <ul className="account-fields">

                        <div className = 'Name'>
                                <label>Username</label>
                                <input type="text" value={this.state.name} onChange={this.onChangeName} />
                        </div>

                        <div className = 'Password'>
                                <label>Password</label>
                                <input type="password" value={this.state.password} onChange={this.onChangePassword} />
                        </div>


                        <div className='submitForm'>
                            <div className='submit' onClick={e=>this.submit()}>Submit</div>
                        </div>

                    </ul>
                </div>

            </InlineCss>




        );
    }
    /**
     * <InlineCss> component allows you to write a CSS stylesheet for your component. Target
     * your component with `&` and its children with `& selectors`. Be specific.
     */
    static css () {
        return (`
            & .Register {
                position: fixed;
                right: 550px;
                bottom: 550px;
                font-size: 50px;
            }
            & .account-fields {
                position: fixed;
                right: 550px;
                bottom: 450px;
                font-size: 20px;
            }
            & .submitForm {
                position: fixed;
                right: 10px;
                bottom: 10px;
                width: 200px;
            }
            & .submit {
                cursor: pointer;
                text-align: center;
                font-size: 20px;
                padding: 10px;
                background-color: #00E4A5;
                color: #fff;
                border-radius: 5px;
                box-shadow: 0 2px 10px 0 rgba(0,0,0,.13);
                border-bottom: 2px solid #00C791;
                text-shadow: 0px -1px 0px #009E73;
            }
        `);
    }

}

export default Transmit.createContainer(Registration);

共有1个答案

金高轩
2023-03-14

如果使用箭头函数,则不需要绑定。

onNameChange = (e)=> {
this.setState({name:e.target.value});
}

onPasswordChange = (e) => {
this.setState({password:e.target.value});
}
 类似资料:
  • 请注意,我在react ProductScreen.js中呈现动态数据时有问题 注意,...数据在product.js中呈现得很好,但相同的数据在productscreen.js中不会呈现,因为productscreen.js链接是通过“id”呈现的。 谢谢app.js 导入“./homescreen.css”;从'../Components/Product'导入产品从'React'导入{useE

  • 通常,当我单击子组件中的菜单项时,它会调用{this.handlesort},这是一个本地函数。句柄排序从父组件接收onReorder道具。{onReorder}调用名为reOrder的本地函数。它设置{orderBy and orderDir}的状态。问题是,当我单击{menuitem}时,它立即返回此错误。(未捕获的TypeError:无法读取未定义的属性“onReOrder”)。通常,当我不

  • 问题内容: 我是新来的反应和反应路由器。 react的问题在于,在我的应用程序的某些页面上,react-router正在运行,并且出现一些类似的错误:“无法读取未定义的属性’push’”。 我正在使用反应0.14.1 我的路由代码如下所示: 我的组件现在是这样的: 谁能帮我这个?谢谢。 问题答案: 您的应用程序中没有提供支持的Router实例。 我假设您要导入withRouter以获取Router

  • 问题内容: 我是一个非常新的反应者,我正在尝试从Rails api导入数据,但出现错误 如果我使用react dev工具,则可以在控制台中看到状态,也可以看到联系人,方法是使用有人可以帮忙解决我做错的事情吗?我的组件看起来像这样: 问题答案: 无法读取未定义的属性“ map”,为什么? 因为最初是,并且of 将是 不确定的 。重要的一点是, componentDidMount 将在初始渲染后被调用