【问题标题】:ReactJS - How to get form value from multiple child componentReactJS - 如何从多个子组件中获取表单值
【发布时间】:2020-05-23 10:56:45
【问题描述】:

我知道这可能有一些类似的问题,但我似乎无法找到适合我情况的解决方案。

我有一个表单,会和子组件的内容一起提交,子组件是onClick附加的,可以无限附加。如何从所有子组件中获取值并发布它。

这是 index.js

class ListOfProducts extends React.Component {
  constructor()
  {
    super();
    this.appendChild = this.appendChild.bind(this);
    this.state = {
      children: [],
      }
  }  

    appendChild() {
      this.setState({
        children: [
          ...this.state.children, <NewComponent/>
        ]
      });
    }

    render() {
      return (
        <form>
          <div>
            <pre><h2 className="h2"> Sales Order</h2></pre>
            <div className="box" style={{height: '520px', width: '1300px', position: 'relative', overflow: 'auto', padding: '0'}}>
            <div style={{height: '1000px', width: '1000px', padding: '10px'}}>

              <div>
                    {this.state.children.map(child => child )}
              </div>
            </div>
            </div>

          <button className="addbut" onClick={() => this.appendChild()}>Add Items</button>
          </div>
        </form>
    )  
  }
}

这是NewComponent.JS的部分代码

            <select
              name="sel"
              className="sel"
              value={this.state.selecteditems}
              onChange={(e) => 
                this.setState({selecteditems: e.target.value})}
            >
            {this.state.data.map(item => 
              <option key={item.productID} value={item.unitPrice}>
                {item.itemName}
              </option>
            )}
            </select>

            {/*unit price*/}
            <p>Unit Price: RM {this.state.selecteditems} </p>

            {this.state.selecteditems.length ? (
              <p>Quantity: </p>
            ) : null }

            {/*button to add quantity*/}
            {this.state.selecteditems.length ? (
              <button onClick={this.addPro}> + </button>
            ) : null }

            {/*textbox for quantity*/}
            {this.state.selecteditems.length ? (
              <input type="text" ref="quan" placeholder="Quantity" 
                value={this.state.quantity}
                onChange={(e) => 
                  this.setState({quantity: e.target.value})}
                >
                </input>
            ) : null }

            {/*button to decrease quantity}*/}
            {this.state.selecteditems.length ? (
              <button onClick={this.decPro}> - </button>
            ) : null }

            {/*subtotal*/}
            {this.state.selecteditems.length ? (
              <p>Sub Total: RM {this.state.subtot} </p>
            ) : null }

提前致谢!

【问题讨论】:

    标签: javascript reactjs forms post components


    【解决方案1】:

    又快又笨:像这样添加回调

    &lt;NewComponent onChange={this.onNewComponentChange}/&gt;

    (当然,在 NewComponent 的每次更改时调用此 onChange 回调)

    【讨论】:

    • 如何在NewComponent调用onChange回调?
    • this.props.onChange()
    • 这是取回值的权利,我要怎么存储呢?
    • 你可以将它存储在ListOfProducts 状态的某个数组中,因为你不喜欢 Redux(即 setState)。
    【解决方案2】:

    我可以想到从子组件获取值的两种方法 -

    1. 拥有一个可以实际存储所有子组件数据的状态管理系统(类似于 redux)。当子组件的数据发生变化时,它应该被同步到存储中,并且父组件可以使用相同的方式在提交时发布数据。
    2. ref 分配给每个附加的子组件。将这些参考值保存在数组中。在提交表单时遍历这些引用,并调用子组件的某些特定 getter 函数来为您提供其数据。

    首选方法是第一种方法。

    【讨论】:

    • 以我追加孩子的方式,我应该如何为每个组件添加参考?
    • 我正在考虑将字符串值分配给 ref,例如“childComponent1”等,但这不是根据新反应版本分配 refs 的首选方式。
    • 除了redux还有其他选择吗?听说很复杂
    猜你喜欢
    • 2020-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-07
    • 1970-01-01
    • 2023-03-24
    • 2016-06-25
    • 1970-01-01
    相关资源
    最近更新 更多