A
A
Alexander Ivanov2018-02-10 11:18:23
React
Alexander Ivanov, 2018-02-10 11:18:23

How to pass value from list to React?

The code is very simple, I think it's too much to describe here

class App extends React.Component{
  state = {
    titel: 'List',
    arr: [
      'simon', 'dima', 'kosty'
    ]
  }
  ren(){
    alert( --- здесь должно быть значение текущего варианта списка --- )
  }
  render(){
    return(
      <div>
        <h1>{this.state.titel}</h1>
        <ol>
          {this.state.arr.map(
            (list, key) =>
              <li onClick={this.ren.bind(this)}>{list}</li>
          )}
        </ol>
      </div> 
    ); 
  }
}

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);

https://codepen.io/Simon1111/pen/LQWBYY?editors=0010
____
ren(){
alert( this.state.arr [ need to pass key value here somehow] )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex Ander, 2018-02-10
@cimonlebedev

class App extends React.Component{
  state = {
    title: 'List',
    arr: [
      'simon','dima','kosty'
    ]
  }

  ren = ({target}) => {
    alert(target.textContent);
  }

  render(){
    return(
      <div>
        <h1>{this.state.title}</h1>
        <ol>
          {this.state.arr.map(
            (list, key) =>
              <li onClick={this.ren} key={key}>{list}</li>
          )}
        </ol>
      </div> 
    ); 
  }
}

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question