简体   繁体   English

将更新的状态值从父级传递给子级。 反应打字稿

[英]Passing an updated state value from Parent to Child. React Typescript

I am kind of new to React Typescript, and I want to pass some data from a parent to a child.我是 React Typescript 的新手,我想将一些数据从父级传递给子级。 So basicly whenever the user clicks on an specific element, then it activates a function in the parents component.所以基本上每当用户点击一个特定的元素时,它就会激活父组件中的一个功能。 When that function runs, it changes the value of an boolean to it's oppostite which works fine.当该函数运行时,它会将布尔值更改为与之相反的值,这可以正常工作。 And then It should pass the value to another child.然后它应该将值传递给另一个孩子。 However for some reason, the value that is passed from the parent to the child never changes.但是由于某种原因,从父级传递给子级的值永远不会改变。 It should work like this:它应该像这样工作:

Child 1 --activateFunction---> Parent --runFunction--> this.state.hideCart = false/true子 1 --activateFunction---> 父 --runFunction--> this.state.hideCart = false/true

Parent --this.state.hideCart--> Child 2 --coverCart = this.props.hideCart--父级 --this.state.hideCart--> 子级 2 --coverCart = this.props.hideCart--

But the value in Child 2 never updates properly, and I can't figure out why.但是 Child 2 中的值永远不会正确更新,我不知道为什么。

      /*CHILD 1*/
export default class Navbar extends Component <{handleCart:any}> {
    render() {
        return (
            <div style={navbar}>
                <h1>Navbar</h1>
                <h4 onClick={() => this.props.handleCart()}>Shopping Cart</h4>
            </div>
        )
    }
}


       /*PARENT*/
export default class Layout extends Component <{fixCart?:boolean}> {

    state = {
        hideCart: true
    }

    render() {
        return (
            <div style={layout}>
                <Navbar handleCart={this.displayCart} />
                <Counters fixCart={this.state.hideCart} />
                <Content />
                <Footer />
            </div>
        )
    }

    displayCart = () => { 
        if (this.state.hideCart == true) {
            this.setState({hideCart: false})
        }
        else {
            this.setState({hideCart: true})
        }
        console.log(this.state.hideCart);

    }
}



        /*CHILD 2*/
export default class Counters extends Component <{fixCart:any},State>{
    state = { 
        counters: [
            { id: "vegetables", value: 1},
        ],
        hideCart: this.props.fixCart
    }
}

The method that you have used leaves the user, as well as code reviewers, confused.您使用的方法让用户和代码审查者感到困惑。 To solve these kinds of data passing functionalities, React-Redux is introduced.为了解决这些类型的数据传递功能,引入了React-Redux Maintain the state variable globally and you would be able to access from anywhere.全局维护状态变量,您就可以从任何地方访问。

And BTW, try checking the values for props that you receive in the child components.顺便说一句,请尝试检查您在子组件中收到的道具的值。 I would suggest you to better use React-Redux-Store.我建议你更好地使用 React-Redux-Store。 Hope it helps!.希望能帮助到你!。

暂无
暂无

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

相关问题 尝试从子级更改父级 state 时,父级 State 未更新。 (将处理程序作为道具从父母传递给孩子) - Parent State is not being updated when trying to change the parent state from child. (passing a handler as prop from parent to child) 将更新的 state 从父母传递给孩子 - Passing updated state from parent to child 如何传递来自不同组件(关于和联系)的道具? 不是父母对孩子。 使用反应 typescript - How to pass props from different component(about and contact)? not parent to child. Using react typescript 反应:将状态从孩子传递给孩子到父母 - React: Passing state from child to child to parent React 将状态从子级传递到父级 - React Passing state from child to parent React,将状态从子组件传递给父组件 - React, passing state from a Child component to the Parent 反应:将 Child1 state 值传递给 Parent 并从 parent 传回 Child2 - React: Passing Child1 state value to Parent and pass back to Child2 from parent 在React中将状态从子级传递到父级; 孩子有单独的状态 - Passing state from child to parent in React; child has separate state 添加类型以在 React 中使用 TypeScript 从父级向子级传递 ref state 设置器 - Add types for passing a ref state setter from parent to child with TypeScript in React 即使将更新的值从父级传递给子级,也不会呈现子级 - Even after passing updated value from parent to child, child is not rendered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM