【问题标题】:Which is the proper way to use State vs Props哪个是使用 State vs Props 的正确方法
【发布时间】:2017-10-02 12:00:54
【问题描述】:

试图找出将数据处理成 React 组件的方式。

我实际上尝试了这两种方法:

A) 州道

componentWillReceiveProps(nextProps) {
if (!nextProps.allPostsQuery.loading && !nextProps.allPostsQuery.error) {
  this.setState({
    postList: nextProps.allPostsQuery.allPosts,
  })
 }
}

render () {
    return (
        <FlatList data={this.state.postList}>
    )
}

B) 道具方式

render () {
    return (
        <FlatList data={this.props.allPostQuery.data}>
    )
}

有什么建议/为什么?

【问题讨论】:

    标签: react-native apollo-client


    【解决方案1】:

    我想你已经实现了来自 GraphQL 的请求,因为 apollo-client 标签。假设如果你没有实现 Redux,那么 state 的方式是确保组件被刷新,因为会触发 render 方法。如果你已经有 Redux,那么 props 方式是最好的方式,因为当你的数据在全局状态发生变化时,会触发 render 方法。

    【讨论】:

      【解决方案2】:

      你可以直接使用 props 的方式。由于您使用的是 apollo-client,因此它已经保存在 redux 商店中。因此,每当来自任何组件的数据有更新时,您也会在此组件中收到它,从而自动触发重新渲染。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-10
        • 1970-01-01
        • 2020-06-16
        相关资源
        最近更新 更多