Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

javascript - Why is my component breaking if I reference a method in `onClick` within a `map`?

The most stupid thing is happening with my code right now. I have a list of items render in the DOM, I need to put a button in order to call another function, if I put the button like this <button></button> everything is ok, but if I assign a function to that button, then everything goes down <button onClick={function}></button> I will show you my code, look

@connectToStores
export default class Dealers extends Component {

  static contextTypes = {
    router : React.PropTypes.func,
  }

  static propTypes = {
    title : React.PropTypes.func,
  }

  constructor (props) {
    super(props);
    this.state = {
      modal : false,
    }
  }

  static getStores () {
    return [ GetDealersStore ];
  }

  static getPropsFromStores () {
    return GetDealersStore.getState();
  }
  render () {
    let dealersInfo;
    if (this.props.dealerData !== null) {
      dealersInfo = this.props.dealerData.dealersData.map(function(dealer) {
        return (<div key={dealer.DealerId} style={Styles.dealerCard}>
              <Card>
                <CardHeader title={dealer.NickName}
                            subtitle={dealer.DealerId}
                            avatar={dealer.Picture}/>
                <CardText>
                  <FloatingActionButton> ////////////////////////
                    <IconAdd />    //////THIS IS THE BUTTON/////
                  </FloatingActionButton>//////////////////////
                </CardText>
              </Card>
            </div>
        );
      });
    } else {
      dealersInfo = <p>Loading . . .</p>;
    }

    return (
      <Grid>
        <Row>
          <Column><h4>Dealers</h4></Column>
        </Row>
        <div style={Styles.mainCont}>
          {dealersInfo}
        </div>
      </Grid>
    );
  }

  componentWillMount () {
    GetDealersActions.getDealers();
  }

  _openUpdateDealer = () => {
    console.log(123);
  }
}

as you can see there is an statement

if (this.props.dealerData !== null) {
   ...
}else {
   dealersInfo = <p>Loading . . .</p>;
}

as I pasted the code above everything works awesome, but if I add <FloatingActionButton onClick={this._openUpdateDealer.bind(this)}><IconAdd /></FloatingActionButton> then everything goes down, all I see in the screen is Loading . . . which is the else in the statement above.

So, I want to know, what is going on with react here ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're rendering the button in the middle of a .map operation:

this.props.dealerData.dealersData.map(function(dealer) {

which uses a different value for this; thus, this.props doesn't exist inside the function. I would expect to see cannot read property dealerData of undefined in the browser console.

You need to use the optional thisArg parameter:

this.props.dealerData.dealersData.map(function(dealer) {
  // ...
}, this);

bind the mapping function to this manually:

this.props.dealerData.dealersData.map(function(dealer) {
  // ...
}.bind(this));

or use an arrow function (since you're using ES6 features):

this.props.dealerData.dealersData.map((dealer) => {
  // ...
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...