简体   繁体   English

如何使用react获取动态创建的输入字段的值

[英]How to get the value of a dynamicly created input field with react

I have been trying to create a dynamic from with React. 我一直在尝试用React创建一个动态的。 So far I managed to show and hide the form without a problem. 到目前为止,我设法毫无问题地显示和隐藏了表单。 I now need to get the values from the created fields and this is causing some problems. 我现在需要从创建的字段中获取值,这引起了一些问题。

This is my code so far. 到目前为止,这是我的代码。

First my dynamic component that gets added when people click a button: 首先,我的动态组件是在人们单击按钮时添加的:

 class MediaInput extends React.Component { render() { const linkName = `link-${this.props.index}`; const contentName = `content-${this.props.index}`; const linkValue = `this.props.${linkName}`; const contentValue = `this.props.${contentName}`; return ( <div> <ControlLabel>Media (optional)</ControlLabel> <input name={ linkName } value={ linkValue } className="form-control" placeholder="Add your media url" type="text" /> <input name={ contentName } value={ contentValue } className="form-control" placeholder="Add your media content" type="text" /> </div> ); } } 

First in the render part I have: 首先在渲染部分,我有:

  const mediaFields = this.state.mediaFields.map((Element, index) => { return <Element key={ index } index={ index } /> }) 

This is the part of my form where it gets rendered: 这是我的表单的一部分,它被呈现:

  <div> { mediaFields } <Button onClick={ () => this.add() }>Add media field</Button> <Button onClick={ () => this.remove() }>Remove media field</Button> </div> 

I want to save these values in the state of the parent component: 我想将这些值保存在父组件的状态中:

 mediaUrls: { "link-0": null, "link-1": null, "link-2": null}, mediaContents: { "content-0": null, "content-1": null, "content-2": null} 

It's a bit messy but I was trying to use this example to get it to work: Example 这是一个有点混乱,但我试图用这个例子来得到它的工作: 实例

I know that the first part, where I set linkValue and contentValue is not correct, but how should I fix it? 我知道在第一部分中设置linkValue和contentValue的位置不正确,但是应该如何解决?

I also know I still need to pass the onChange method to the element but that is the next step. 我也知道我仍然需要将onChange方法传递给元素,但这是下一步。

UPDATE: 更新:

I think this would be a cleaner way of doing it but now I have a different problem. 我认为这将是一种更清洁的方式,但是现在我遇到了另一个问题。

I create an array where I want to store the values. 我在要存储值的位置创建一个数组。 I am able to read them in the child but how do I now save the changes in the parent? 我可以在孩子中阅读它们,但是现在如何将更改保存在父母中? How can I read the index of the child in the parent so I can save it in the right place? 如何在父母中读取孩子的索引,以便将其保存在正确的位置?

 class MediaInput extends React.Component { render() { const linkName = `link${this.props.index}`; const contentName = `content${this.props.index}`; return ( <div> <ControlLabel>Media (optional)</ControlLabel> <input onChange={this.props.handleChange} name={ linkName } value={ this.props.mediaUrls[this.props.index]} className="form-control" placeholder="Add your media url" type="text" /> <input name={ contentName } value={ this.props.mediaContents[this.props.index]} className="form-control" placeholder="Add your media content" type="text" /> </div> ); } } 

My state: 我的状态:

  mediaUrls: [ '', '', ''], mediaContents: ['', '', ''], 

How should my handleChange function look if I want to setState every time the input changes? 如果每次输入更改时都想设置状态,我的handleChange函数应该如何显示? I want to read the index and change my array based on that. 我想读取索引并基于此更改数组。

 // Handle media fields handleChange(e){ const mediaUrls = this.state.mediaUrls; // based on the index, change the value in the array this.setState({ ??? }); } 

To get the index to the parent for updating, simply pass it in the callback. 要将索引获取给父级进行更新,只需在回调中传递它即可。

class MediaInput extends React.Component {
  // ...
  render() {
    return (
      <div>
      <ControlLabel>Media (optional)</ControlLabel>
        <input
        onChange={(event) => this.props.handleChange(event, this.props.index)} />

When dealing state in React, treat it as immutable. 在React中处理状态时,请将其视为不可变的。

handleChange(e, index) {
    // Shallow copy of array
    const mediaUrls = this.state.mediaUrls.slice();
    mediaUrls[index] = e.target.value;
    this.setState({mediaUrls: mediaUrls});
});

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

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