1

_renderRow() 関数内で状態を呼び出そうとしていますが、次のエラーが発生し続けます。

ここに画像の説明を入力

これは私のソース コードです:
ソース コード

 var Store = require('./store/Store.js');
 var MessageOptions = require('./MessageOptions.js')

 var React = require('react');
 var ReactNative = require('react-native');
 var {
   AppRegistry,
   Image,
   ListView,
   StyleSheet,
   Text,
   View,
 } = ReactNative;


var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2})

 class Application extends React.Component {
   constructor(props) {

   super(props);

   this.state = {
      selectedRowID: 20,
      dataSource: ds
   }
}


componentWillMount() {

  this.getNewMessages();
}


getNewMessages() {

   Store.getMessages().then((messages) => {

       this.setState({
         dataSource: this.state.dataSource.cloneWithRows(messages)
       });

   },(reason) => {

       console.log("Error:", reason);
   });
}


_renderRow(rowData: string, sectionID: number, rowID: number, highlightRow: (sectionID: number, rowID: number) => void) {

  var currentSelectedRowID = this.state.selectedRowID;
  return (

        <View>
           <View style={styles.row}>
              <Image style={styles.thumb} source={require('../android/app/src/main/res/mipmap-hdpi/ic_launcher.png')} />
              <Text style={styles.text}>
                 {rowData.text}
              </Text>
           </View>
           <MessageOptions optionsData={rowData.options} message_uri={rowData.uri}/>
        </View>

   )
}


render() {
  return (
     <ListView
       dataSource={this.state.dataSource}
       renderRow={this._renderRow}
     />
  )
}
};


var styles = StyleSheet.create({
  row: {
    flexDirection: 'row',
    justifyContent: 'center',
    padding: 10,
    backgroundColor: 'white',
  },
  thumb: {
    width: 64,
    height: 64,
  },
  text: {
    flex: 1,
  },
  news_item: {
    paddingLeft: 10,
    paddingRight: 10,
    paddingTop: 15,
    paddingBottom: 15,
    marginBottom: 5
  },
   news_item_text: {
    color: '#575757',
    fontSize: 18
  }
});


module.exports = Application;

エラーは、this.state.selectedRowID を var currentSelectedRowID に格納している _renderRow メソッドから発生しています。

前もって感謝します。

4

1 に答える 1

2

ソリューション
問題は、私の ES6 クラス コンストラクターにありました。

class Application extends React.Component {
  constructor(props) {

   super(props);
   this._renderRow = this._renderRow.bind(this);

   this.state = {
      selectedRowID: 20,
      dataSource: ds
   }
}

解決策は、コンストラクターに次の行を追加することでした。

this._renderRow = this._renderRow.bind(this);
于 2016-07-15T14:55:33.010 に答える