无法在本机的render()中使用this.state

时间:2019-09-10 14:02:18

标签: react-native

在componentDidMount函数中,我正在执行fetch(),将响应存储在我的状态。

我还添加了其他一些对象,例如isLoaded: true

如果我执行console.log(this.state.isLoaded);,则在render()中,将出现以下错误。

TypeError: null is not an object (evaluating 'this.state.isLoaded')

Stack trace:
  App.js:38:27 in render
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:11581:21 in finishClassComponent
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:11509:4 in updateClassComponent
  ...
ERROR
09:58
Warning: %s: Error boundaries should implement getDerivedStateFromError(). In that method, return a state update to display an error message or fallback UI., RootErrorBoundary

Stack trace:
  node_modules/react-native/Libraries/YellowBox/YellowBox.js:59:8 in error
  node_modules/expo/build/environment/muteWarnings.fx.js:26:24 in error
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:638:36 in warningWithoutStack
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:15918:16 in callback
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:13857:16 in callCallback
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:13901:19 in commitUpdateEffects
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:13892:22 in commitUpdateQueue
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:15123:10 in commitLifeCycles
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:16636:8 in commitAllLifeCycles
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:93:15 in invokeGuardedCallbackImpl
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:318:36 in invokeGuardedCallback
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:16876:8 in commitRoot
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18501:17 in <unknown>
  node_modules/scheduler/cjs/scheduler.development.js:255:23 in unstable_runWithPriority
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18499:4 in completeRoot
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18427:21 in performWorkOnRoot
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18324:24 in performWork
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18285:14 in performSyncWork
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18169:19 in requestWork
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:17969:16 in scheduleWork
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:18642:15 in scheduleRootUpdate
  node_modules/react-native/Libraries/Renderer/oss/ReactNativeRenderer-dev.js:19512:20 in render
  node_modules/react-native/Libraries/ReactNative/renderApplication.js:61:52 in renderApplication
  node_modules/react-native/Libraries/ReactNative/AppRegistry.js:104:10 in run
  node_modules/react-native/Libraries/ReactNative/AppRegistry.js:198:26 in runApplication
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:366:47 in __callFunction
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:106:26 in <unknown>
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:314:10 in __guard
  node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:105:17 in callFunctionReturnFlushedQueue

否则,如果我这样做了,console.log(this.state);将会打印状态,并且我将在Expo的登录中看到isLoaded: true

什么?为什么?为什么看不到this.state.isLoaded

1 个答案:

答案 0 :(得分:5)

也许您没有定义状态。应该是这样的:

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