简体   繁体   中英

How to access variable in React.js component class

If I have

const stateData = [
{
    'id': 'AL',
    'name': 'Alabama',
    'capital': 'Montgomery',
    'date': 'December 14, 1819'
}, 
{
    'id': 'AK',
    'name': 'Alaska',
    'capital': 'Juneau',
    'date': 'January 3, 1959'
}];

const SearchBarAndResults = React.createClass({
  render() {
    // QUESTION: how do I access variable stateData here and replace field
    // from 'id': 'AL' to 'id': 'AK'?
  }
});

You should pass the const stateData in to your component like this example, then you have access to the component scope data using {this.props.data} in your component.

React.render(<SearchBarAndResults data={stateData[0]} />, document.getElementById('container'));

This is a working example.

http://jsfiddle.net/diegochavez/5q644d53/2/

Best,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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