【问题标题】:react-native componentWillUpdate, componentWillReceiveProps - Warningreact-native componentWillUpdate,componentWillReceiveProps - 警告
【发布时间】:2019-12-18 15:44:58
【问题描述】:

反应原生 (componentWillUpdate, componentWillReceiveProps)

滑动功能应用。

  • 警告:componentWillReceiveProps 已弃用,将在下一个主要版本中删除。请改用静态 getDerivedStateFromProps。

  • 警告:componentWillUpdate 已弃用,将在下一个主要版本中删除。请改用 componentDidUpdate。作为临时解决方法,您可以重命名为 UNSAFE_componentWillUpdate。

YellowBox.ignoreWarnings 方法不是必需的。

我会要求你更新代码。

我该如何解决?

//git component
const renderPagination = (index, total, context) => {

  return (
    <View style={styles.paginationStyle}>
      <Text style={{ color: 'grey' }}>
        <Text style={styles.paginationText}>{index + 1}</Text>/{total}
      </Text>
    </View>
  )
}

export default class App extends Component {

  constructor(props) {

    super(props);

    this.onPressNext = this.onPressNext.bind(this);
    this.onPressPrev = this.onPressPrev.bind(this);

    this.state = {
      indexPage: 0
    }
  }


  onPressPrev = () => {

    const { indexPage } = this.state;

    if (indexPage > 0) {
      this.refs.swiper.scrollBy(-1);
    }
  }

  onPressNext = () => {

    const { indexPage } = this.state;

    if (indexPage < 4) {
      this.refs.swiper.scrollBy(1);
    }
  }

  render() {
    return (
      <View style={styles.container}>
        <View style={{flex:0.1, backgroundColor: 'green'}}>
          <Text>NAVTEX</Text>
        </View>

        {/* {git component} */}
        <Swiper
          style={styles.wrapper}
          onIndexChanged={indexPage => this.setState({ indexPage })}
          renderPagination={renderPagination}
          showsButtons={false}
          loop={false}
          ref={'swiper'}
        >

          <View style={styles.slide}>
            <Text style={styles.text}>2</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>2</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>3</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>4</Text>
          </View>
          <View style={styles.slide}>
            <Text style={styles.text}>5</Text>
          </View>
        </Swiper>

        <View style={styles.buttoncontainer}>
          <Button
            style={{with:75}}
            onPress={this.onPressPrev}
            title="Previous">
          </Button>
          <Button
            onPress={this.onPressNext}
            title="Next">
          </Button>
        </View>
      </View>
    );
  }
}

【问题讨论】:

    标签: react-native react-lifecycle


    【解决方案1】:

    警告不是由您的代码引起的。这是由react-native-swiper 库引起的。我在 GitHub 上查看了他们的代码,在第 199 行的 src/index.js 文件中,他们调用了 componentWillReceiveProps()。这不是您需要担心的事情,而是库维护者的责任。

    Screenshot of search on GitHub

    【讨论】:

    • 谢谢。多亏了这个,我找到了确切的原因。你必须使用其他组件来解决这个问题吗?
    • 不应该可以安全使用。这些方法确实被认为是遗留的,但目前还没有什么会破坏。库的维护者可能会更新方法。无论如何,这是他们的责任,而不是你的。
    【解决方案2】:

    我似乎还不能发表评论,所以我将把这个答案留在这里。我有同样的问题。正如您在 hongs 的 cmets 答案中提到的,您没有使用过 componentWillUpdatecomponentWillReceiveProps,但您安装的模块之一可能正在使用它们。在我的情况下,它是 react-momentmomentjs 使用 componentWillUpdate。同样如上所述,它将在下一个即将到来的主要更新中从 react-native 中删除,因此强烈建议使用替代方案。

    或者,您可以禁用警告框。但不建议在开发环境中这样做。如果您愿意,可以通过将此 console.disableYellowBox = true; 放在根组件中来禁用警告。

    附带说明一下,请将所有样式移到样式表上,而不是内联样式,因为稍后再更改会很麻烦。

    快乐编码:)

    更新:正如你所提到的,你使用了react-native-swiper,当然它确实同时使用了componentWillReceivePropscomponentWillUpdate。如此处所示。

    on line 199: componentWillReceiveProps(nextProps) on line 217: componentWillUpdate(nextProps, nextState)

    是的,只要您不使用这些方法,您的代码就没有问题。请使用更新的方法。至于 react-native-swiper,开发人员可能会在下一次主要更新中删除之前对其进行更新,以便您可以在下一次主要版本中进行npm update 并检查。

    【讨论】:

    • 我使用的唯一组件是 Swiper(react-native-swiper)。它最有可能发生在这里吗?而且我的代码没有问题?
    • 嗨。请检查更新的答案。如果有帮助,请选择最佳答案。祝你好运:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2020-01-06
    • 2020-06-06
    • 2023-04-03
    • 1970-01-01
    相关资源
    最近更新 更多