我和React工作了一年多,我读过
Thinking in react,Lifting state up和
State and lifecycle.
我了解到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个使用该子组件的具体父级,是否每个父级对于他们使用的同一个孩子具有相同的初始化子状态?