简体   繁体   English

更新Redux存储时,React不重新渲染

[英]React not re-rendering when I update my redux store

I know it's me... Because I suck :) 我知道是我...因为我很烂:)

But I believe I am doing everything correct but when I dispatch my action and the state changes my view does not re-render. 但是我相信我所做的一切都是正确的,但是当我分派行动并且状态改变时,我的观点不会重新呈现。

I believe this is the simple change that might be needed but the 我相信这是可能需要的简单更改,但

render() render()

render(){
    ....
    <div className={data.participants}>
        +{store.getState().currentSex.participants}
    </div>
    ....
}

Function that calls the action 调用动作的函数

onSetParticipants = () => {
    console.info(store.getState()); //participants = 1
    store.dispatch (currentSex.setParticipants(3));
    console.info(store.getState()); //participants = 3
}

reducer currentSex.js 减速器currentSex.js

import { List, Map } from 'immutable';

const initialState = {
    participants: 1
};

function currentSex (state = initialState, action) {
    switch (action.type) {
        case 'SET_PARTICIPANTS':
            return {
                ...state,
                participants:action.participants
            }
    }
    return state
}

export default currentSex;

Actions 动作

export const SET_PARTICIPANTS = 'SET_PARTICIPANTS';

export function setParticipants(participants) {
    return {
        type: SET_PARTICIPANTS,
        participants: participants
    }
}

How I have done my connect, as I believe this helps 我如何建立联系,因为我相信这会有所帮助

function mapStateToProps(state, ownProps) {
    return {
        errorMessage: state.errorMessage,
        inputValue: ownProps.location.pathname.substring(1)
    }
}

export default connect(mapStateToProps, { })(App)

Please forgive me is this isn't enough or completely the wrong information. 请原谅我这是不够或完全错误的信息。

But why does my page no re-render when i can see the state has changed? 但是,当我看到状态已更改时,为什么我的页面没有重新渲染?

edit 编辑

Its worth mentioning that my state has objects inside of it: 值得一提的是,我的州里面有一些物体:

在此处输入图片说明

In your mapStateToProps you need to add the state that you want to render inside your component. mapStateToProps您需要在组件内部添加要渲染的状态。 In this case it looks like state.participants . 在这种情况下,它看起来像state.participants

function mapStateToProps(state, ownProps) {
    return {
        errorMessage: state.errorMessage,
        participants: state.participants,
        inputValue: ownProps.location.pathname.substring(1)
    }
}

And then use this.props.participants in your component. 然后在您的组件中使用this.props.participants

import * as actions from '../actions';

/* ... */

render(){
    ....
    <div className={data.participants}>
        +{this.props.participants}
    </div>
    ....
}

/* ... */

export default connect(mapStateToProps, actions)(App)   

edit 编辑

And add the actions to your connect function, as well as importing them. 并将动作添加到您的connect函数,以及导入它们。 Call your actions using this.props.currentSex(3) inside the function within your component that handles change events. 使用组件中处理更改事件的函数内的this.props.currentSex(3)调用操作。

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

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