Redux状态更改后,componentDidUpdate不触发

时间:2019-04-22 21:07:08

标签: reactjs redux react-redux

我有这些减速器:

const initialState = {
    categories: [],
    programms: {}
}

export const programmReducers = (state = initialState, action) => {
    let programms = state.programms;

    switch (action.type) {
        case actionTypes.FETCH_CATEGORIES:
            return Object.assign({}, state, {
                categories: action.payload
            })
        case actionTypes.FETCH_PROGRAMM:
            programms[action.payload.id] = action.payload;
            console.log(programms);
            return {
                ...state,
                programms: Object.assign({}, programms)
            }
        case actionTypes.FETCH_PROGRAMM_COMPONENTS:
            programms[action.programmId].components = action.payload;
            console.log('Added Components')
            return {
                ...state,
                programms: programms
            }
        default:
            return state
    }
}

最后一个(FETCH_PROGRAMM_COMPONENTS)将数组添加到programm对象中的对象。这行得通,但是某种程度上它不会在我的组件中触发componentDidUpdate。不过,它适用于FETCH_PROGRAMM

class ProgrammPage extends Component {

    static async getInitialProps({ store, query: {id} }) {
        let programm;
        if (!store.getState().programm.programms[id]) {
            console.log('Programm not! found');
            programm = await store.dispatch(loadProgramm(id));
            await store.dispatch(loadProgrammComponents(id));
        } else {
            programm = store.getState().programm.programms[id];
            console.log('Programm found')
        }

        return {
            // programm: programm
            programmId: id
        }
    }

    componentDidUpdate(prevProps) {
        console.log('UPDATE', this.props, this.props.programm.components.length)
        if (!prevProps.user && this.props.user) {
            this.props.loadProgrammComponents(this.props.programmId);
        }
    }

    render() {
        return (
            <div>
                <h1>Programm</h1>
                <h2>{this.props.programm.name}</h2>
                <h2>{this.props.programm.id}</h2>
                <h3>Components: {this.props.programm.components ? this.props.programm.components.length : 'None'}</h3>
                <br></br>
                <h1>User: { this.props.user ? this.props.user.uid : 'None'}</h1>
                <button onClick={() => this.props.loadProgramm('ProgrammLevel2')}>Load Programm</button>
                <button onClick={() => this.props.loadProgrammComponents(this.props.programmId)}>Load Components</button>

            </div>
        )
    }
}

function mapStateToProps(state) {
    return {
        programm: state.programm.programms['ProgrammLevel1'],
        programms: state.programm.programms,
        user: state.auth.user
    }
}

const mapDispatchToProps = dispatch => bindActionCreators({
    loadProgrammComponents,
    loadProgramm
}, dispatch)

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(ProgrammPage)

1 个答案:

答案 0 :(得分:1)

您返回相同的参考。

尝试返回programms数组的副本:[...programms](如果是Object.assign(),则返回Object)。

    case actionTypes.FETCH_PROGRAMM_COMPONENTS:
        programms[action.programmId].components = action.payload;
        console.log('Added Components')
        return {
            ...state,
            programms: [...programms]  // <-- Return new state
        }
相关问题