简体   繁体   English

反应子组件不更新父状态,所以第二个孩子没有注意到变化

[英]React child component not updating parents state, so second child is not noticing the changes

Curently I have this structure.目前我有这个结构。

Child1 <---- Parent ----> Child2

Parent component has this state父组件有这个状态

    this.state = {
            authenticationChecked: true,
            isAuthenticated: true,
            sideBarRetracted: false
        }

this.retract = this.retract.bind(this);
        this.unretract = this.unretract.bind(this);

and the following functions以及以下功能

retract() {
    this.setState({
        sideBarRetracted: true
    }, () => {
        console.log("retracted")
        console.log(this.state.sideBarRetracted) //I can see this changing to true, but the child wont take it into effect, even tho if i hardcode it , it will
    });
}

unretract() {
    this.setState({
        sideBarRetracted: false
    })
    console.log("unretracted")
}

Which is pased to both childs传递给两个孩子

<SideBar retract={this.retract} unretract={this.unretract} retracted={this.state.sideBarRetracted} {...this.props}/> 
<PrivateRoute authed={this.state.isAuthenticated} path="/skadi" render={(props) => <Skadi retracted={this.state.sideBarRetracted} {...props}/>} />

Now my question is.现在我的问题是。 I supposedly should receive that as a prop in a child component, for example lets say in the sidebar component which is a child.我应该将它作为子组件中的道具接收,例如让我们在侧边栏组件中说它是一个子组件。 Whose constructor is this, and im supposed to receive the prop there and assign it to the state这是谁的构造函数,我应该在那里接收道具并将其分配给状态

   constructor(props) {
    super(props);
    this.state = {
        retracted: this.props.retracted
    }

}

It works properly so far, but now I have the following issue.到目前为止它工作正常,但现在我有以下问题。 This same component has a function onClick() which will change that specific state同样的组件有一个 onClick() 函数,它会改变那个特定的状态

       <FontAwesomeIcon className="registerIcon" icon={faArrowAltCircleLeft} onClick={this.props.retract} />
<FontAwesomeIcon className="expandicon-retracted" icon={faArrowAltCircleRight} onClick={this.props.unretract} />

But point is, this is not working, the state is not being updated in the parent component.但重点是,这不起作用,状态没有在父组件中更新。 I can change it manually (hardcoding) and I see the child component reacting to the change, but i cant make it react with the onclick() update我可以手动更改它(硬编码)并且我看到子组件对更改做出反应,但我不能让它对 onclick() 更新做出反应

At the same time, I have my Child2 component, who has been passed the parent state as well.同时,我有我的 Child2 组件,它也已经通过了父状态。 Will its state update if I update the parent component using CHILD1 ?如果我使用 CHILD1 更新父组件,它的状态会更新吗?

I have seen that i should use something like this in the child2 , but well it wont work so far (mainly because parent component isnt updated afaik)我已经看到我应该在 child2 中使用这样的东西,但到目前为止它不会工作(主要是因为父组件没有更新 afaik)

   constructor(props) {
    super(props)
    this.state = {
        sideretracted: false
    }
    console.log("props in skadi")

}

componentWillReceiveProps(nextProps) {
    this.setState({ sideretracted: nextProps.sideBarRetracted});
  }

You shouldn't set the child state from the props received from the parent component.您不应该从从父组件接收到的道具设置子状态。 Since you are updating the state of child component from the methods bound to the child components it isn't reflected in the parent component.由于您正在从绑定到子组件的方法更新子组件的状态,因此它不会反映在父组件中。 One thing you could do is pass a method from the parent to update the state of the parent component and the child state gets updated by itself since it will be passed the new state variable from parent as a prop.您可以做的一件事是从父组件传递一个方法来更新父组件的状态,并且子状态会自行更新,因为它将从父组件作为道具传递新的状态变量。 You could have the toggleRetract method defined in the parent to set the parent state and pass it as a prop to the child component which would trigger it and set the parent state for you which will be passed down to the children components as props (like how you are passing the state props).您可以在父toggleRetract定义toggleRetract方法来设置父状态并将其作为道具传递给子组件,这将触发它并为您设置父状态,该状态将作为道具传递给子组件(例如你正在传递状态道具)。

For example例如

function Toggle() {
  const [on, setOn] = React.useState(false)
  const toggle = () => setOn(o => !o)
  return <Switch on={on} onToggle={toggle} />
}
function Switch({on, onToggle}) {
  return (
    <div>
      <div>The button is {on ? 'on' : 'off'}</div>
      <button onClick={onToggle}>Toggle</button>
    </div>
  )
}

Here Toggle is the parent component and Switch is the child component and the parent component passes the on state as well as onToggle method which the child component uses to display and update the state respectively.这里 Toggle 是父组件,Switch 是子组件,父组件传递on状态以及子组件用来显示和更新状态的onToggle方法。 More info here更多信息在这里

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

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