简体   繁体   English

反应:将 Child1 state 值传递给 Parent 并从 parent 传回 Child2

[英]React: Passing Child1 state value to Parent and pass back to Child2 from parent

In my app I have two child components, 'FoodScreen', 'OperationScreen' and a parent Component 'Desk'.在我的应用程序中,我有两个子组件,“FoodScreen”、“OperationScreen”和一个父组件“Desk”。 Am passing a Json variable array to FoodScreen component to select the item.我将 Json 变量数组传递给 FoodScreen 组件到 select 项目。

When I press the button in Child1 component, it should pass the value for parent, and then the parent should pass the value for child2当我按下 Child1 组件中的按钮时,它应该传递父级的值,然后父级应该传递 child2 的值

Desk Component as:桌面组件为:

const nameList = [
        {
            "id": 111,
            "name": "Leanne Graham",
            "username": "Bret"
            }
        },
        {
            "id": 112,
            "name": "Ervin Howell",
            "username": "Antonette"
            }
        }
    ];

    let selectedItem = null;
    let item = (selectedItem == null) ? 0 : nameList.find(x => x.id === selectedItem);

class SalesDesk extends Component {

    render() {
        return <div>
            <div className="Background">
                <Header/>
                <div className="Main">
                    <FoodScreen itemlist={nameList}/>
                    <OperationScreen item={item}/>
                </div>
                <Footer/>
            </div>
        </div>
    }
} 

where a 'select ID' state is updated by a click event in the 'FoodScreen Component', like so:其中“选择 ID”state 由“FoodScreen 组件”中的单击事件更新,如下所示:

class SdFoodScreen extends Component {
  render() {
    return <div className="sdFoodScreenMain">
        {
            this.props.itemlist.map(items =>
                <div className="sdFoodBoxes" key={items.id}>
                  {items.id}
                  {items.name}
                  <button onClick={() => this.selectItem(items.id)}> Select ID </button>
                </div>
            )
        }
    </div>;
  }

  constructor(props) {
    super(props);
    this.state = {
      selectedItem : null
    }
  }

  selectItem(itemID) {
    this.setState({
      selectedItem : itemID
    })
  }
}

And My 2nd Child component as:我的第二个子组件为:

class OperationScreen extends Component {
  render() {
    let selectedItem = this.props.item;
    console.log(selectedItem);
  }
}

Whenever I click the button in FoodScreen component, the value should be passed to the Parent 'Salesdesk' component, so I could go pass the value to Child2 component, and print on the console.每当我单击 FoodScreen 组件中的按钮时,该值应传递给父“Salesdesk”组件,因此我可以 go 将值传递给 Child2 组件,并在控制台上打印。

I would prefer I there is a way to get rid of the If condition in the Parent class, to have better code structure, as am new to React.我希望我有一种方法可以摆脱父 class 中的 If 条件,以获得更好的代码结构,就像 React 的新手一样。

You will need to pass a callback function to a child element to update a value in the parent class.您需要将回调 function 传递给子元素以更新父 class 中的值。 You can restructure it in the following way -您可以通过以下方式对其进行重组 -

In FoodScreen , update the onClick handler to call the callback function and pass the item as an argument -FoodScreen中,更新onClick处理程序以调用回调 function 并将项目作为参数传递 -

...
<button onClick={() => this.props.onSelect(item)}> Select ID </button>
...

Then on the SalesDesk class, you can pass the callback handler while using the FoodScreen component然后在SalesDesk class 上,您可以在使用FoodScreen组件时传递回调处理程序

<FoodScreen itemlist={nameList} onSelect={this.updateSelection}/>

and you can update the selected item using this new callback function并且您可以使用此新回调 function 更新所选项目

updateSelected = (item) => {
  this.setState({selectedItem: item});
}

now you can pass selectedItem to OperationScreen现在您可以将selectedItem传递给OperationScreen

<OperationScreen item={selectedItem}/>

EDIT:编辑:

See Demo看演示

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

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