我只是在制作一个简单的应用程序来学习与 redux 的异步。我已经让一切正常,现在我只想在网页上显示实际状态。现在,我如何在渲染方法中实际访问商店的状态?
这是我的代码(所有内容都在一页中,因为我只是在学习):
const initialState = {
        fetching: false,
        fetched: false,
        items: [],
        error: null
    }
const reducer = (state=initialState, action) => {
    switch (action.type) {
        case "REQUEST_PENDING": {
            return {...state, fetching: true};
        }
        case "REQUEST_FULFILLED": {
            return {
                ...state,
                fetching: false,
                fetched: true,
                items: action.payload
            }
        }
        case "REQUEST_REJECTED": {
            return {...state, fetching: false, error: action.payload}   
        }
        default: 
            return state;
    }
};
const middleware = applyMiddleware(promise(), thunk, logger());
const store = createStore(reducer, middleware);
store.dispatch({
    type: "REQUEST",
    payload: fetch('http://localhost:8000/list').then((res)=>res.json())
});
store.dispatch({
    type: "REQUEST",
    payload: fetch('http://localhost:8000/list').then((res)=>res.json())
});
render(
    <Provider store={store}>
        <div>
            { this.props.items.map((item) => <p> {item.title} </p> )}
        </div>
    </Provider>,
    document.getElementById('app')
);
因此,在 state 的 render 方法中,我想列出item.title商店中的所有内容。
谢谢