【问题标题】:React native app performance on connect(mapStateToProps, mapDispatchToProps)在连接上反应本机应用程序性能(mapStateToProps,mapDispatchToProps)
【发布时间】:2021-08-23 02:09:22
【问题描述】:

我正在使用 redux 状态管理创建反应原生应用程序。我想知道使用 connect(mapStateToProps, mapDispatchToProps) 的最佳做法是什么。

我有几个组件类,即 ParentA、ChildA、ChildB。目前我正在独立获取每个父类和子类的状态属性。

例如:

const ParentA = (props) => {
  return (
    <View>
      <Text>{props.item.name}</Text>
      <ChildA />
      <ChildB />
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default connect(mapStateToProps)(ParentA)
const ChildA = (props) => {
  return (
    <View>
      <Text>{props.item.name}</Text>
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default connect(mapStateToProps)(ChildA)
const ChildB = (props) => {
  return (
    <View>
      <Text>{props.item.age}</Text>
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default connect(mapStateToProps)(ChildB)

但我可以从ParentA 获取item 状态并将其传递给Child 组件,而不是为每个子组件设置connect

例如:

const ParentA = (props) => {
  return (
    <View>
      <Text>{props.item.name}</Text>
      <ChildA item={item}/>
      <ChildB item={item}/>
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default connect(mapStateToProps)(ParentA)
const ChildA = (props) => {
  return (
    <View>
      <Text>{props.item.name}</Text>
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default ChildA
const ChildB = (props) => {
  return (
    <View>
      <Text>{props.item.age}</Text>
    </View>
  )
}

const mapStateToProps = (state) => {
  const { item } = state

  return {
    item: item.item,
  }
}

export default ChildB

我的问题是,

  1. 在考虑应用性能时,最好的方法是什么?
  2. 我可以对mapDispatchToProps 使用相同的方法吗?

【问题讨论】:

标签: react-native redux react-redux mapstatetoprops mapdispatchtoprops


【解决方案1】:

我认为与其使用“const”,不如尝试其他数据类型,例如“var”或“let”,因为“const”值一旦固定就无法更改。

【讨论】:

    【解决方案2】:

    是的,你可以使用useSelector, useDispatch,但问题是你应该使用钩子。它可以通过这种方法修复考虑应用程序性能的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-09
      • 2022-06-11
      • 2020-03-14
      • 2021-02-05
      • 2021-01-05
      • 2017-10-13
      相关资源
      最近更新 更多