【问题标题】:react props not updating with redux storereact props 不使用 redux store 更新
【发布时间】:2017-10-20 06:57:59
【问题描述】:

我一直使用 react-redux connect 来配置 props,但我需要使用 react Component 来使用生命周期方法。我注意到我从商店获取的道具似乎是静态的,并且它们不会随着商店的更新而更新。

代码:

class AlertModal extends Component {

  title
  isOpen
  message

  componentDidMount() {
    const { store } = this.context
    const state = store.getState()
    console.log('state', state)
    console.log('store', store)
    this.unsubscribe = store.subscribe(() => this.forceUpdate())
    this.title = state.get('alertModal').get('alertModalTitle')
    this.isOpen = state.get('alertModal').get('isAlertModalOpen')
    this.message = state.get('alertModal').get('alertModalMessage')
    this.forceUpdate()
  }

  componentWillUnmount() {
    this.unsubscribe()
  }

  updateAlertModalMessage(message) {
    this.context.store.dispatch(updateAlertModalMessage(message))
  }
  updateAlertModalTitle(title) {
    this.context.store.dispatch(updateAlertModalTitle(title))
  }

  updateAlertModalIsOpen(isOpen) {
    this.context.store.dispatch(updateAlertModalIsOpen(isOpen))
  }

  render() {

    console.log('AlertModal rendered')
    console.log('AlertModal open', this.isOpen) <======= stays true when in store it is false

    return (
      <View

如何设置 titleisOpenmessage 以便它们始终反映商店价值?

【问题讨论】:

  • this.forceUpdate() 需要什么?
  • 使用 connect 将你的组件连接到 redux store。然后使用 mapStateToProps 获取更新后的 props。
  • @Codesingh 在代码的更下方有一个视图,它使用onLayout 来获取视图的高度。然后它使用高度设置内容样式。这就是为什么我需要在安装后调用forceUpdate - 因为第一个渲染没有高度。

标签: reactjs react-native redux react-redux


【解决方案1】:

这可能对你有帮助

componentWillReceiveProps(nextProps) {

 console.log();
 this.setState({searchData : nextProps.searchData})
}

【讨论】:

    【解决方案2】:

    对我来说,问题是我将 this.props.myObject 分配给了一个没有深度克隆的变量,所以我使用

    修复了它
    let prev = Object.assign({}, this.props.searchData)
    

    我在做什么

    let prev = this.props.searchData
    

    所以我打扰了整个页面。对我来说似乎很安静。

    【讨论】:

    • 另外,Object.assign(this.props.searchData, newData) 而不是Object.assign({}, this.props.searchData, newData) 是常见的错误
    【解决方案3】:

    应该是这样的。在您的确认组件中:

    const mapStateToProps = (state) => {
      return { modalActive: state.confirmation.modalActive };
    }
    
    export default connect(mapStateToProps)(Confirmation);
    

    在你的 reducer 索引文件中,应该是这样的:

    const rootReducer = combineReducers({
      confirmation: ConfirmationReducer
    });
    

    我相信您在这里有自己的名为 ConfirmationReducer 的 reducer 文件。应该是这样的。

    import { ON_CONFIRM } from '../actions';
    
    const INITIAL_STATE = {modalActive: true};
    export default function(state = INITIAL_STATE, action) {
      console.log(action);
      switch (action.type) {
        case ON_CONFIRM:
          return { ...state, modalActive: action.payload };
      }
    
      return state;
    }
    

    确保您编写自己的动作创建器来创建具有上述类型和布尔类型相关负载的动作。

    最后,您应该能够像这样从您的 Confirmation 组件内的商店访问该属性:

    {this.props.modalActive}
    

    您还没有发布完整的代码,因此很难为确切的场景提供解决方案。希望这可以帮助。编码愉快!

    【讨论】:

    • 是的,关键是我需要继续使用我已经放弃的 react-redux connect。如果不是 connected,则 props 不会随 redux 存储更新。谢谢。
    • 是的,当然,这就是连接助手或 HOC 的用武之地。无论如何,我很高兴你做到了!
    • 对于我的问题,它没有意识到状态被划分为相应的减速器。谢谢@RavindraRanwala
    猜你喜欢
    • 2020-08-10
    • 2016-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多