在升级到16.3版之前,我会根据props 的变化调用一个方法,如下所示:

componentWillReceiveProps(nextProps){
   if(this.props.country.length !== nextProps.country){
    doSomething(); //example calling redux action
    }
}

componentWillReceiveProps16.3版上不安全,我们必须使用getDerivedStateFromProps.然而,这个方法返回一个对象,我不知道如何从内部调用一个方法,就像在componentWillReceiveProps中那样

推荐答案

是的,您需要返回一个对象,它是从nextProp派生的新状态.根据文件:

getDerivedStateFromProps应该返回一个要更新状态的对象,或者返回null以指示新props 不需要任何状态更新.

But由于您在componentWillReceiveProps中没有以任何方式更新您的状态,您应该使用componentDidUpdate而不是getDerivedStateFromProps:

componentDidUpdate(prevProps){
  if ( prevProps.country !== this.props.country.length ) {
    doSomething(); //example calling redux action
  }
}

React-native相关问答推荐

条件呈现Else语句不会将样式应用于我的组件

ReactNative如何在填充编辑表单 timeshift 除按钮禁用状态

如何判断 Detox 正在运行测试?

react-native redux 和 ListView

你如何使 react-native react-navigation 标签栏透明

如何在 React Native 中使用断点进行调试

没有找到 `React-fishhook` 的 podspec

如何删除不需要的expo 模块

如何使 React Native Animated.View 可点击?

React-native Xcode 构建失败 -> 'RCTAssert.h file not found'

react-native run-android 构建错误':app:generateDebugBuildConfig'

React Native TextInput blur TouchableHighlight 按下事件

无法判断模块react-native-maps:找不到名称为 default的配置

使用来自另一个页面的 fetch 调用函数返回结果值

React Native - Firebase 身份验证持久性不起作用

DeviceInfo 原生模块未正确安装

React Native Navigation 组件路由问题

按钮的标题属性必须是字符串

我将如何根据百分比为按钮的宽度设置动画,并且它的背景 colored颜色 也是如此?

运行 expo start 时如何禁用在浏览器中打开 DevTools?