简体   繁体   English

React Native&Redux:为什么子组件不会在每次状态更新时重新呈现?

[英]React Native & Redux: Why aren't child components re-rendering on every state update?

In React Native and Redux, I have a <NavigationCardStack/> as a root component. 在React Native和Redux中,我有一个<NavigationCardStack/>作为根组件。 And every time state has been updated, the redux-logger updates correctly with the next/new state. 每次状态更新时,redux-logger都会在下一个/新状态下正确更新。

And after the state change, when newly updated state is console logged in a child component, it doesn't console log the updated state but rather console logs the initial state (in the child component childPage.js and logging: render(){ console.log(this.props.state) return(...) ... ). 并且在状态更改之后,当新更新的状态是控制台登录子组件时,它不会控制日志记录更新的状态,而是控制台记录初始状态(在子组件childPage.js和logging: render(){ console.log(this.props.state) return(...) ... )。

Could it be that I am hooked up to Redux incorrectly or missing something? 可能是因为我错误地连接到Redux或者丢失了某些东西? Because, everything seem to work perfectly fine and makes sense. 因为,一切看起来都很完美,也很有意义。

Thank you in advance! 先感谢您!

Here are some snippets of my code: 以下是我的代码的一些片段:

This is my reducer and the child component would just log the initialState here even though other properties have been added and updated: 这是我的reducer,子组件只会在这里记录initialState,即使已经添加和更新了其他属性:

const initialState = {
  meetUp: false,
}

function itemReducer(state = initialState, action) {
  switch(action.type) {


    case ITEM_QUANTITY:
      return {
        ...state,
        quantity: action.quantity
      }

    case ITEM_PRICE:
      return {
        ...state,
        price: action.price
      }

    case ITEM_MEET_UP:
      return {
        ...state,
        meetUp: action.meetUp
      }

     default:
       return state
  }
}

export default itemReducer

And connected to the root component like so: 并连接到根组件,如下所示:

function mapStateToProps(state) {
  return {
    itemInfo: state.itemReducer,
    ...
  }
}

export default connect(
  mapStateToProps,
  {
    itemQuantity: (value) => itemQuantity(value),
    itemPrice: (value) => itemPrice(value),
    itemMeetUp: (value) => itemMeetUp(value),
  }
)(NavigationRoot)

With following actions: 通过以下操作:

export function itemMeetUp(value) {
  return {
    type: ITEM_MEET_UP,
    meetUp: value
  }
}

export function itemQuantity(value) {
  return {
    type: ITEM_QUANTITY,
    quantity: value
  }
}

export function itemPrice(value) {
  return {
    type: ITEM_PRICE,
    price: value
  }
}

state={this.props} represent the state and how it is supposed to be passed down to child components state = {this.props}表示状态以及如何将其传递给子组件

  _renderScene (props) {
    const { route } = props.scene

    return (
      <route.component _handleNavigate={this._handleNavigate.bind(this)} state={this.props}/>
    )
  }

    <NavigationCardStack
      renderScene={this._renderScene}
      ...
    />

Could this issue be related? 这个问题可能有关系吗? It seems similar in that NavigationCardStack won't update even though the redux state changes, and redux-logger logs the change. 看起来类似的是,即使redux状态发生变化, NavigationCardStack也不会更新,而redux-logger记录更改。 I can't tell from your code sample, but one way to test this might be to connect the children or one child directly to your redux store instead of having NavigationCardStack pass the store state in as props? 我无法从您的代码示例中看出,但是测试它的一种方法可能是connect子项或一个子项直接连接到您的redux商店而不是让NavigationCardStack将商店状态作为道具传递? That github issue above also has some hacky workarounds, but I'm not very familiar with react-native so I can't be much help there. 上面的github问题也有一些hacky解决方法,但我对反应原生不是很熟悉所以我在那里帮不了多少。

This is basically a stab in the dark and based entirely on our conversation in the comments, but is the _renderScene method bound to the component in the constructor? 这基本上是在黑暗中刺伤并完全基于我们在注释中的对话,但是_renderScene方法是否绑定到构造函数中的组件? Eg: 例如:

constructor (props) {
  super(props)
  ...
  this._renderScene = this._renderScene.bind(this);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM