Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 反应状态不一致_Javascript_Meteor_Reactjs - Fatal编程技术网

Javascript 反应状态不一致

Javascript 反应状态不一致,javascript,meteor,reactjs,Javascript,Meteor,Reactjs,我要实现的目标:将数据从孩子传递给家长 我如何尝试实现它:使用此。按说明说明 不知道如何拼写标题:当我在修改状态的函数中输入console.log(this.state)时,会打印出this.state中的正确值。但是,当我试图读取另一个函数中的状态时,this.state显然仍然是空值 constructor(props) { super(props); this.state = { titleInputValue: "", }; } <form o

我要实现的目标:将数据从孩子传递给家长

我如何尝试实现它:使用
此。按说明说明

不知道如何拼写标题:当我在修改状态的函数中输入console.log(this.state)
时,会打印出
this.state
中的正确值。但是,当我试图读取另一个函数中的状态时,
this.state
显然仍然是空值

constructor(props) {
    super(props);
    this.state = {
      titleInputValue: "",
    };
}

<form onSubmit={this.handleSubmit.bind(this)}>
    <TextInput
        val={this.state.titleInputValue}
        changeHandler={this.textInputChangeHandler} />
</form>


// This is the function which can apparently only get the initial state
// title and body are empty strings, they should have values
handleSubmit(event) {
    event.preventDefault();
    const title = this.state.titleInputValue;
    const body = this.state.bodyInputValue;
    console.log(this.state);
    Meteor.call('stories.insert',title,body);
}

// However, here the state is retrieved just fine.
// This is the function the child calls to update the state.
textInputChangeHandler(event) {
    // This console.log call shows the correct state!
    console.log(this.state);
    this.setState({
      titleInputValue: event.target.value,
    });
}
构造函数(道具){
超级(道具);
此.state={
标题输入值:“”,
};
}
//这是一个显然只能得到初始状态的函数
//标题和正文是空字符串,它们应该有值
handleSubmit(事件){
event.preventDefault();
const title=this.state.titleInputValue;
const body=this.state.bodyInputValue;
console.log(this.state);
Meteor.call('stories.insert',title,body);
}
//然而,在这里,状态被很好地检索到。
//这是子级调用以更新状态的函数。
textInputChangeHandler(事件){
//此console.log调用显示正确的状态!
console.log(this.state);
这是我的国家({
titleInputValue:event.target.value,
});
}
TextInput
具有属性
onChange={this.props.changeHandler.bind(this)}

举例说明:


我编写了
asd
,并且在
textInputChangeHandler
中成功地检索到了状态,这是前两个
控制台。记录
调用,但在
handleSubmit

中它是空的,这是因为事件处理程序范围不是组件类级别的。当您的组件处理事件时,它的上下文是组件(在您的情况下是TextInput)而不是父级

您必须将该函数绑定到此组件类范围:

<TextInput
        val={this.state.titleInputValue}
        changeHandler={this.textInputChangeHandler.bind(this) } />


使用JavaScript绑定还可以指定函数上下文。

这是因为事件处理程序的作用域不是组件类级别。当您的组件处理事件时,它的上下文是组件(在您的情况下是TextInput)而不是父级

您必须将该函数绑定到此组件类范围:

<TextInput
        val={this.state.titleInputValue}
        changeHandler={this.textInputChangeHandler.bind(this) } />


使用JavaScript绑定,您还可以指定函数上下文。

谢谢,先生,它现在可以工作了!你的答案一正确,我就给你打分available@Gudmundur我很高兴听到你这么说。谢谢你,先生,现在可以用了!你的答案一正确,我就给你打分available@Gudmundur听到这个消息我很高兴。