简体   繁体   English

将数据从子级传递到父级并设置状态

[英]Passing data from child to parent and setting state

i am a child and parent component where i am trying to pass data from child to parent but the issue is after passing the data and setting a state to it i get nothing in console when i console.log it the first time but then after clicking the submit button second time i am getting the result or say it is running behind 1 step.. 我是一个孩子和父母的组件,我试图将数据从孩子传递给父母,但是问题是在传递数据并将状态设置为它之后,当我第一次进行console.log记录时,控制台中什么也没有,但是单击后第二次提交按钮,我得到结果,或者说它正在落后一步。

I did it like this 我是这样做的

parent component 父组件

import Todoinput from './components/todo-input';


class App extends React.Component{
constructor(props){
super(props);
this.state={
  item:''
}
}

onsearch=(term)=>{

console.log(term)
this.setState({item:term})
console.log(this.state.item)
}

render(){
return (
  <div className="app">
  <div className="todoinput">
  <Todoinput onafter={this.onsearch}/>
  </div>
  </div>
) 
} 
}

export default App;

so here after submitting the form when in onsearch function i try to console log (this.state.item) for the very first time i get empty result but in the same function when i first console log (term) i am getting the result 所以在提交表单后,当在onsearch函数中时,我第一次尝试控制台日志(this.state.item)我得到空结果,但是当我第一次控制台日志(term)时却在同一个函数中,我得到结果

Output: 输出:

在此处输入图片说明

child component 子组件

class Todoinput extends React.Component{
constructor(props){
    super(props);
    this.state={
        term:''
    }
}

onformsubmit=(e)=>{
    e.preventDefault()

    this.props.onafter(this.state.term)
}

onchange=(e)=>{
    this.setState({term:e.target.value})


}

render(){
    return (

        <div className="card p-4">
        <form onSubmit={this.onformsubmit}>
        <div className="form-group">
        <input type="text" onChange={this.onchange} 
   className="form-control" placeholder="Add a Todo Item"/>
        </div>
        <button type="submit" className="btn btn-block btn- 
   primary">Submit</button>
        </form>
        </div>

    )
 }
}
 export default Todoinput;

this.setState({item:term}) is asynchronous; this.setState({item:term})是异步的; so the statement console.log(this.state.item) get executed before setState finishes executing. 因此,在setState完成执行之前,先执行console.log(this.state.item)语句。

To log the value after setState has completed, you can pass a function as second parameter to get the state correctly after completion like this: 要在setState完成后记录该值,可以将一个函数作为第二个参数传递,以在完成后正确获取状态,如下所示:

this.setState({item:term}, () => console.log(this.state.item));

That probably happens because state is "asynchronous". 那可能是因为状态是“异步的”。 To console log after the state is set, you should call a callback function in this.setState... 要在设置状态后控制台日志,您应该在this.setState中调用回调函数...

onsearch=(term)=>{
 console.log(term)
  this.setState({item:term}, () => {
    console.log(this.state.item)
 })
}

I hope this helps 我希望这有帮助

So its important to remember in React that setState is asynchronous (checkout that link its very informative) So your state value is getting set, but it isn't immediately available after setting it in that function because its async but your function is synchronous. 因此,重要的是要记住在React中setState是异步的 (检出该链接可提供非常有用的信息)。因此,将设置您的状态值,但是在该函数中设置状态值后,该状态值不会立即可用,因为它是异步的,但您的函数是同步的。 if you were to plop that console.log into the componentDidUpdate() method you will see that calling setState() will re-render the component with the new state variable; 如果将那个console.log放到componentDidUpdate()方法中,您将看到调用setState()将使用新的状态变量重新呈现该组件;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM