最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to update input text which is render by array map in REACTJS - Stack Overflow

programmeradmin0浏览0评论
const data = [
  {
    title: "Hello World",
    pany: [
      "Google",
      "Apple",
      "Facebook"
    ]
  }
];

class App extends React.Component {
  render() {
    return (
      <ul>
        {data[0]pany.map((item, index) => (
          <input type="text" key={index} value={item}></input>
        ))}
      </ul>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

it renders perfectlly but when i am trying to edit its not editing the text. (not editable) and if i add one more input to same array with blank area value it also not working (not updated).

const data = [
  {
    title: "Hello World",
    pany: [
      "Google",
      "Apple",
      "Facebook"
    ]
  }
];

class App extends React.Component {
  render() {
    return (
      <ul>
        {data[0].pany.map((item, index) => (
          <input type="text" key={index} value={item}></input>
        ))}
      </ul>
    );
  }
}

ReactDOM.render(<App />, document.getElementById("app"));

it renders perfectlly but when i am trying to edit its not editing the text. (not editable) and if i add one more input to same array with blank area value it also not working (not updated).

Share Improve this question asked Dec 13, 2018 at 15:28 rohitrohit 3096 silver badges13 bronze badges 2
  • 1 add a onchange event. read more here stackoverflow./questions/41736213/… – Code Maniac Commented Dec 13, 2018 at 15:35
  • What @CodeManiac said. Your browser console should warn you about it. – Gbacc Commented Dec 13, 2018 at 15:37
Add a ment  | 

3 Answers 3

Reset to default 5

You should use state and controlled ponents for this. I strongly remend you actually go through React's main concepts that are displayed to the right on their website as it will make your development process much easier.

To apply the controlled ponent principle to your current code, you need to have an onChange event bound to your input:

class App extends React.Component {
  state = {
    title: "Hello World",
    panies: [
      "Google",
      "Apple",
      "Facebook"
    ],
  };

  updateCompany(newName, index) {
    const { panies } = this.state;
    const newCompanies = [...panies];
    newCompanies[index] = newName;
    this.setState({ panies: newCompanies });
  }

  render() {
    const { panies } = this.state;
    return (
      <ul>
        {panies.map((item, index) => (
          <input type="text" key={index} value={item} onChange={(e) => this.updateCompany(e.target.value, index)}></input>
        ))}
      </ul>
    );
  }
}

Try the following :)

class App extends React.Component {
  state = {
    panies: data[0].pany,
  };

  updateText = (e, index) => {
    const panies = [...this.state.panies];
    panies[index] = e.target.value

    this.setState({ panies });
  }

  render() {
    return (
      <ul>
        {this.state.panies.map((item, index) => (
          <input
            type="text"
            value={item}
            onChange={(e) => this.updateText(e, index)}
          />
        ))}
      </ul>
    );
  }
}

Try the following code:

const data = [   {
    title: "Hello World",
    pany: [
      "Google",
      "Apple",
      "Facebook"
    ]  
    } 
];

class App extends React.Component {   
  handleChange = (e) => {
    const target = e.target;
    const value = target.value;
    const name = target.name;
    data[0].pany[+name] = value
  }

  render() {
    return (
      <ul>
        {data[0].pany.map((item, index) => (
          <input type="text" onchange={this.handleChange} name={""+index} key={index} value={item}></input>
        ))}
      </ul>
    );   
  } 
}

ReactDOM.render(<App />, document.getElementById("app"));

I transform the index into input name and listen to the change on the input and I transform the index to an integer to replace the value in the array

发布评论

评论列表(0)

  1. 暂无评论