Pass item data to a react modal

前端 未结 3 1585
伪装坚强ぢ
伪装坚强ぢ 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条回答
  •  -上瘾入骨i
    2021-02-15 00:03

    Try this: this is the form which has the button, and is a child component of some other component that passes the handleButtonAction method as props, and the button takes the input data and invokes this parent component method

    handleSubmit = (e) => {
      e.preventDefault();
      const data = e.target.elements.option.value.trim();
      if (!data) {
        this.setState(() => ({ error: 'Please type data' }));
      } else {
        this.props.handleButtonAction(data, date);
      }
    } 
    
    {this.state.error && 

    {this.state.error}

    }

    The parent component:

    handleButtonAction = (data) => {
    
      axios.get(`http://localhost:3000/someGetMethod/${data}`).then(response => {
            const resData = response.data;
            this.setState({
                openModal: true,
                status: response.status,
                data: resData
             });
        }).catch((error) => {
            if (error.message.toLowerCase() === 'network error') {              
              this.setStateWithError(-1, {});
            }
            else { // not found aka 404              
              this.setStateWithError(error.response.status, '', {currency, date: ddat});
            }
        });        
    }
    

提交回复
热议问题