Pass item data to a react modal

前端 未结 3 1586
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 23:46

I have a map that render few items and one of its line is below

 this.setState({\"openDeleteModal\":true)}>Delete
3条回答
  •  无人共我
    2021-02-15 00:09

    When working on React applications, try not to think in terms of passing values to other components, but rather updating state that your components are exposed to. In your example, assuming your modal component is a child of the same component your list of a tags belongs to, you could set the values you are interested in exposing to the modal on the state, as well as updating the property that signals whether the modal is open or not. For example:

    class Container extends React.Component {
        constructor(props) {
           super(props)
           this.state = {
              openDeleteModal: false,
              activeItemName: '', //state property to hold item name
              activeItemId: null, //state property to hold item id
           }
        }
    
        openModalWithItem(item) {
           this.setState({
              openDeleteModal: true,
              activeItemName: item.name,
              activeItemId: item.id
           })
        }
    
        render() {
    
        let buttonList = this.props.item.map( item => {
          return (
        });
    
        return (
         
    {/* Example Modal Component */} { buttonList }
    ) } }

提交回复
热议问题