简体   繁体   English

React-每个子组件的更新状态

[英]React - Updating state for each child component

Not sure if i'm understanding this correctly. 不知道我是否理解正确。 If i'm making a child component, type button, that increments its own counter, is it possible to do so without having 2 separate functions? 如果我要创建一个子组件,请键入按钮,这会增加其自己的计数器,是否可以在没有2个单独功能的情况下这样做? the following i've done seems like a hack? 我完成的以下操作似乎很简单? what if theres X amount of buttons, how would i refactor this code to be more dynamic? 如果按钮数量为X怎么办,我将如何重构此代码以使其更加动态? REF's seem to work in the way i can reference the html in the child, but what about the other way? REF似乎以我可以在孩子中引用html的方式工作,但是另一种方式呢? Am i even thinking about this the right way, because component has its own state, should it have its own update method? 我是否还在考虑这种正确方法,因为组件具有自己的状态,是否应该具有自己的更新方法?

 /*** @jsx React.DOM */ var MyComponent = React.createClass({ getInitialState: function() { return { counter1: 1, counter2: 1 }; }, increment: function(i) { if (i === 1) { this.setState({ counter1: this.state.counter1 + 1 }); } else { this.setState({ counter2: this.state.counter2 + 1 }); } }, render: function() { return ( < div > <ChildComponent item = {this.state.counter1} click={this.increment.bind(this, 1)}/> <ChildComponent item={this.state.counter2} click={this.increment.bind(this, 2)}/ > < /div> ); } }); var ChildComponent = React.createClass({ render: function() { return ( <div> <h1> Counter {this.props.item} </h1 > <button onClick = {this.props.click} > ++ < /button> </div > ); } }); React.render( < MyComponent / > , document.body); 

I see in your comment that you've settled on putting state in the child component. 我在您的评论中看到,您已经决定将状态放在子组件中。 While that works fine, the power of React is that you reason about state at a level that makes sense for all the interactions in the application. 尽管这很好用,但是React的功能是让您在对应用程序中所有交互都有意义的级别上进行状态推理。 It is perfectly reasonable to hold both counters' state in the parent. 将两个计数器的状态都保留在父级中是完全合理的。 I think the typical implementation would have a single update function in the parent, pass it, current counter value, and the counter IDs down as props, and have an update function in the child designed to invoke the parent's update function with the relevant counter ID. 我认为典型的实现将在父级中具有单个更新功能,将其,当前计数器值和计数器ID向下传递为prop,并在子级中具有一个update函数,该子级旨在通过相关计数器ID调用父级的update函数。 。

Update: Your gist implementing this pattern is close to what I was talking about, keeping state in the parent and creating an onClick handler in the child that in turn calls its parent's update function, passing in parameters that let the parent know which counter to update. 更新:实现此模式的要点接近我在说的内容,将状态保留在父级中,并在子级中创建onClick处理程序,进而调用其父级的update函数,并传入使父级知道要更新哪个计数器的参数。 You may find it more useful to just pass the "refz" prop as that parameter rather than passing in the entire child React component, but as a proof of concept, you have the idea. 您可能会发现,仅将“ refz”道具作为该参数传递而不是传递整个子React组件会更有用,但是作为概念证明,您有想法。

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

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