RN TypeError: _this3.setState is not a function

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/DeMonliuhui/article/details/102742607

problem

TypeError: _this3.setState is not a function
    at E:\ITCode\ReactNative\DailyStory\src\views\main\bill\billSearch\index.tsx:72
    at tryCallOne (E:\ITCode\ReactNative\DailyStory\node_modules\promise\setimmediate\core.js:37)
    at E:\ITCode\ReactNative\DailyStory\node_modules\promise\setimmediate\core.js:123
    at E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:289
    at _callTimer (E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146)
    at _callImmediatesPass (E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194)
    at Object.callImmediates (E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458)
    at MessageQueue.__callImmediates (E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:366)
    at E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135
    at MessageQueue.__guard (E:\ITCode\ReactNative\DailyStory\node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:343)

It encapsulates a function using the selector assembly to facilitate global call, in order to obtain the selection result of the use of Promisecallbacks by value.
Then a callback function this.setStateto modify variable values, then an error as shown above, the calling code as follows.

_showDatePicker() {
    //封装了子组件的函数
    TimeUtils.showDatePicker()
      .then((date: string) => {
        this.setState({date: date}); //报错地方
        this.initData();
      })
      .catch(error => {
        console.log(error);
      });
  }

   
    onPress={this._showDatePicker}  //点击调用函数

the reason:

this points to a problem. TimeUtils.showDatePicker () This function gets the value of the sub assembly is returned, and the need to modify the internal state of the parent component. Then this would mean sub-components rather than the parent component, it will report this error.

solution:

Plus a bind when passed into the function

 onPress={this._showDatePicker.bind(this)}

reference

https://blog.csdn.net/ling_du/article/details/99840430

Guess you like

Origin blog.csdn.net/DeMonliuhui/article/details/102742607