【问题标题】:Pass item data to a react modal将项目数据传递给反应模式
【发布时间】:2023-03-05 18:35:02
【问题描述】:

我有一张地图,可以渲染几个项目,其中一条线在下面

<a onClick={()=> this.setState({"openDeleteModal":true)}>Delete</a>

显然我想在用户单击删除时打开一个模式,但是我必须传递一些东西,比如项目的名称、项目的 ID 来执行删除。如何将名称传递给模态?

我可以像这样将obj名称绑定到a 删除

我走对了吗?

【问题讨论】:

    标签: javascript reactjs ecmascript-6


    【解决方案1】:

    试试这个:这是有按钮的表单,是其他组件的子组件,它通过handleButtonAction方法作为props,按钮接受输入数据并调用这个父组件方法

    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 && <p>{this.state.error}</p>} 
    <form onSubmit={this.handleSubmit}>
      <input type="text" name="option"/>
      <div>
        <button>Get data</button>
      </div>
    </form>
    

    父组件:

    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});
            }
        });        
    }
    

    【讨论】:

      【解决方案2】:

      How to pass props to a modal复制我的回答

      类似的场景

      constructor(props) {
      
          super(props)
      
      
          this.state = {
            isModalOpen: false,
            modalProduct: undefined,
          }
      }
      
      //****************************************************************************/
      
      render() {
      
          return (
              <h4> Bag </h4>
              {this.state.isModalOpen & (
                <Modal 
                  modalProduct={this.state.modalProduct}
                  closeModal={() => this.setState({ isModalOpen: false, modalProduct: undefined})
                  deleteProduct={ ... }
                />
              )
      
              {bag.products.map((product, index) => (
              <div key={index}>
                  <div>{product.name}</div>
                  <div>£{product.price}</div>
                  <div>
                  <span> Quantity:{product.quantity} </span>
                  <button onClick={() => this.props.incrementQuantity(product, product.quantity += 1)}> + </button>
                  <button onClick={() => this.decrementQuantity(product)}> - </button> // <----
                  </div>
              </div>
              ))}
          )
      }
      
      //****************************************************************************/
      
      decrementQuantity(product) {
      
          if(product.quantity === 1) {
              this.setState({ isModalOpen: true, modalProduct: product })
          } else {
              this.props.decrementQuantity(product)
          }
      }
      

      【讨论】:

        【解决方案3】:

        在处理 React 应用程序时,尽量不要考虑将值传递给其他组件,而是要更新组件所暴露的状态。 在您的示例中,假设您的模态组件是您的 a 标签列表所属的同一组件的子组件,您可以设置您有兴趣在状态上向模态公开的值,以及更新发出信号的属性模态是否打开。例如:

        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 (<button onClick={() => this.openModalWithItem(item)}>{item.name}</button>
            });
        
            return (
             <div>
                {/* Example Modal Component */}
                <Modal isOpen={this.state.openDeleteModal}  
                       itemId={this.state.activeItemId}
                       itemName={this.state.activeItemName}/>
                { buttonList }
             </div>
            )
            }
        }
        

        【讨论】:

        • 好东西,但我正在渲染一个列表并使用了一个常见的模式。我不认为这是一个很好的例子。
        • 这显然是一个简单的例子——但你可以把这个概念应用到你的用例中——在这个例子中,我有效地呈现了一个按钮列表,所有这些按钮都会触发渲染方法。取决于你的应用程序有多大——我会考虑在这样的状态下使用 Redux。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        • 1970-01-01
        • 2020-01-05
        • 2013-07-21
        • 1970-01-01
        • 2016-01-22
        • 1970-01-01
        相关资源
        最近更新 更多