我了解到React的数据流概念是单向数据流.
来自这些页面的Citates:
React’s one-way data flow (also called one-way binding) keeps everything modular and fast.
Remember: React is all about one-way data flow down the component hierarchy. It may not be immediately clear which component should own what state. This is often the most challenging part for newcomers to understand,so follow these steps to figure it out:…
If you imagine a component tree as a waterfall of props,each component’s state is like an additional water source that joins it at an arbitrary point but also flows down.
据我所知,不允许使用以下示例,因为我将子状态数据传递给父级.但我看到一些开发人员这样工作:
class Parent extends React.Component { constructor(props) { super(props); this.state = { fromParent: null }; } addSomething(stateValueFromChild) { this.setState({fromParent: stateValueFromChild}); } render() { return <Child addSomething={(stateValueFromChild) => this.addSomething(stateValueFromChild)}> // ... </Child>; } } class Child extends React.Component { constructor(props) { super(props); this.state = { fromChild: 'foo' }; } render() { return <Form onSubmit={() => this.props.addSomething(this.state.fromChild)}> // ... </Form>; } }
我现在的问题是:
>这真的不允许吗?
>为什么这不是模块化和快速的?
>这是否真的制动了单向数据流,成为双向数据流?
>这种方式可能会发生什么其他问题?
>当我举起国家时,你会如何解决以下案件; 50个使用该子组件的具体父级,是否每个父级对于他们使用的同一个孩子具有相同的初始化子状态?
有一篇很好的文章详细解释了这一点:Props down,Events Up
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。