Redux存储成功更新,但组件的mapStateToProps接收旧状态

时间:2018-08-08 19:09:12

标签: reactjs redux react-redux

不知道是什么问题,我已经尝试了一切。 这个问题最接近我,但是对我没有帮助 https://github.com/reduxjs/redux/issues/585 减速器

        export default (state = [], action: any) => {
            switch (action.type) {
                case "GET_ALL":
                    return [...action.sections];
                case "ADD_ONE":
                    return [...state, action.section];
                case "REMOVE":
                    return state.filter((section: Section) => section.id !== action.id);
                default:
                    return [...state]
            }
        }

动作创建者

export function loadAll(id: number) {
    return function(dispatch: Dispatch) {
        return fetch(`http://localhost:4000/test`, {  
                method: 'GET',
            })
            .then((response: any) => response.json())
            .then((sections: Section[]) => {
                dispatch({
                    type: "GET_ALL",
                    sections
                });
            },
            err => {
                 throw new Error("Network error");
            }
        );
    } }

应用程序引导程序

const composedEnhancers = compose(
    applyMiddleware(thunk, routerMiddleware(history)),
);

const store = createStore(
    connectRouter(history)(combineReducers({
            sections: sectionReducer, 
        })),
    initialState, //is equal to {}
    composedEnhancers
);

    const target = document.querySelector('#root');

    render(
        <Provider store={store}>
            <ConnectedRouter history={history}>
          <div>
            <main className="appContent">
                <Route exact={true} path="/" component={TestPage} />  
            </main>
          </div>
            </ConnectedRouter>
        </Provider>,
        target
    );

组件连接-在我的道具中,我接收到旧数据,直到我转到下一页并返回

 const mapDispatchToProps = (dispatch: Dispatch, props: any) => {
    return {
        saveSection: (section: Section) => dispatch(saveSection(section) as any),
        loadAll: (id: number) => dispatch(loadAll(id) as any),
        removeSection: (sectionId: number) => dispatch(removeSection(sectionId) as any),
    }
}
const mapStateToProps = ((state: any, ownProps: any) => (() => {
return {
    sections: state.sections || [],
}
}));

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(TestPage)

0 个答案:

没有答案