Reactjs:如何修改子状态或道具从父?

前端之家收集整理的这篇文章主要介绍了Reactjs:如何修改子状态或道具从父?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我基本上试图使选项卡在反应,但有一些问题。

这里是文件page.jsx

  1. <RadioGroup>
  2. <Button title="A" />
  3. <Button title="B" />
  4. </RadioGroup>

当您单击按钮A时,RadioGroup组件需要取消选择按钮B.

“Selected”只是指来自状态或属性的className

这里是RadioGroup.jsx:

  1. module.exports = React.createClass({
  2.  
  3. onChange: function( e ) {
  4. // How to modify children properties here???
  5. },render: function() {
  6. return (<div onChange={this.onChange}>
  7. {this.props.children}
  8. </div>);
  9. }
  10.  
  11. });

Button.jsx的源并不重要,它有一个常规的HTML单选按钮,触发本机DOM onChange事件

预期流量为:

>点击按钮“A”
>按钮“A”触发onChange,原生DOM事件,这会触发RadioGroup
> RadioGroup onChange侦听器被调用
> RadioGroup需要取消选择按钮B.这是我的问题。

这里是我遇到的主要问题:我不能移动< Button> s到RadioGroup,因为这是结构是这样的孩子是任意的。也就是说,标记可以

  1. <RadioGroup>
  2. <Button title="A" />
  3. <Button title="B" />
  4. </RadioGroup>

要么

  1. <RadioGroup>
  2. <OtherThing title="A" />
  3. <OtherThing title="B" />
  4. </RadioGroup>

我试过几件事。

尝试:在RadioGroup的onChange处理程序:

  1. React.Children.forEach( this.props.children,function( child ) {
  2.  
  3. // Set the selected state of each child to be if the underlying <input>
  4. // value matches the child's value
  5.  
  6. child.setState({ selected: child.props.value === e.target.value });
  7.  
  8. });

问题:

  1. Invalid access to component property "setState" on exports at the top
  2. level. See http://fb.me/react-warning-descriptors . Use a static method
  3. instead: <exports />.type.setState(...)

尝试:在RadioGroup的onChange处理程序:

  1. React.Children.forEach( this.props.children,function( child ) {
  2.  
  3. child.props.selected = child.props.value === e.target.value;
  4.  
  5. });

问题:没有发生,即使我给Button类一个componentWillReceiveProps方法

尝试:我试图传递父的一些特定状态给孩子,所以我可以只更新父状态,让孩子自动响应。在RadioGroup的render函数中:

  1. React.Children.forEach( this.props.children,function( item ) {
  2. this.transferPropsTo( item );
  3. },this);

问题:

  1. Failed to make request: Error: Invariant Violation: exports: You can't call
  2. transferPropsTo() on a component that you don't own,exports. This usually
  3. means you are calling transferPropsTo() on a component passed in as props
  4. or children.

解决方案#1:使用react-addons.js cloneWithProps方法在呈现时克隆RadioGroup中的子代,以便能够传递它们的属性

坏的解决方案#2:实现围绕HTML / JSX的抽象,以便我可以动态传递属性(杀了我):

  1. <RadioGroup items=[
  2. { type: Button,title: 'A' },{ type: Button,title: 'B' }
  3. ]; />

然后在RadioGroup中动态构建这些按钮。

This question不帮助我,因为我需要渲染我的孩子,而不知道他们是什么

我不知道为什么你说使用cloneWithProps是一个坏的解决方案,但这里是一个使用它的工作示例。
  1. var Hello = React.createClass({
  2. render: function() {
  3. return <div>Hello {this.props.name}</div>;
  4. }
  5. });
  6.  
  7. var App = React.createClass({
  8. render: function() {
  9. return (
  10. <Group ref="buttonGroup">
  11. <Button key={1} name="Component A"/>
  12. <Button key={2} name="Component B"/>
  13. <Button key={3} name="Component C"/>
  14. </Group>
  15. );
  16. }
  17. });
  18.  
  19. var Group = React.createClass({
  20. getInitialState: function() {
  21. return {
  22. selectedItem: null
  23. };
  24. },selectItem: function(item) {
  25. this.setState({
  26. selectedItem: item
  27. });
  28. },render: function() {
  29. var selectedKey = (this.state.selectedItem && this.state.selectedItem.props.key) || null;
  30. var children = this.props.children.map(function(item,i) {
  31. var isSelected = item.props.key === selectedKey;
  32. return React.addons.cloneWithProps(item,{
  33. isSelected: isSelected,selectItem: this.selectItem,key: item.props.key
  34. });
  35. },this);
  36.  
  37. return (
  38. <div>
  39. <strong>Selected:</strong> {this.state.selectedItem ? this.state.selectedItem.props.name : 'None'}
  40. <hr/>
  41. {children}
  42. </div>
  43. );
  44. }
  45.  
  46. });
  47.  
  48. var Button = React.createClass({
  49. handleClick: function() {
  50. this.props.selectItem(this);
  51. },render: function() {
  52. var selected = this.props.isSelected;
  53. return (
  54. <div
  55. onClick={this.handleClick}
  56. className={selected ? "selected" : ""}
  57. >
  58. {this.props.name} ({this.props.key}) {selected ? "<---" : ""}
  59. </div>
  60. );
  61. }
  62.  
  63. });
  64.  
  65.  
  66. React.renderComponent(<App />,document.body);

这是一个jsFiddle显示它在行动。

编辑:这里有一个更完整的例子与动态标签内容jsFiddle

猜你在找的React相关文章