Reactjs 访问Redux中的存储?
我在react和redux中有一个使用immutablejs的项目。看起来是这样的:Reactjs 访问Redux中的存储?,reactjs,redux,immutable.js,Reactjs,Redux,Immutable.js,我在react和redux中有一个使用immutablejs的项目。看起来是这样的: function mapStateToProps(state) { return { content: state.content } } function mapDispatchToProps(dispatch) { return { actions: bindActionCreators(thunks, dispatch) }; } class Connect
function mapStateToProps(state) {
return {
content: state.content
}
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(thunks, dispatch)
};
}
class ConnectedConnect extends Component {
componentDidMount() {
const { actions } = this.props
actions.retrieveContent();
console.log(this.props.content)
}
render() {
return (
<div>
<Header heading="Demo Heading" colour="orange" disableAnimation={false} />
</div >
);
}
}
`const Content = connect(mapStateToProps, mapDispatchToProps)`(ConnectedConnect);
export default Content
const history = createBrowserHistory()
const store = createStore(
connectRouter(history)(rootReducer),
applyMiddleware(
routerMiddleware(history),
thunkMiddleware
)
);
export { store, history };
页面如下所示:
function mapStateToProps(state) {
return {
content: state.content
}
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(thunks, dispatch)
};
}
class ConnectedConnect extends Component {
componentDidMount() {
const { actions } = this.props
actions.retrieveContent();
console.log(this.props.content)
}
render() {
return (
<div>
<Header heading="Demo Heading" colour="orange" disableAnimation={false} />
</div >
);
}
}
`const Content = connect(mapStateToProps, mapDispatchToProps)`(ConnectedConnect);
export default Content
const history = createBrowserHistory()
const store = createStore(
connectRouter(history)(rootReducer),
applyMiddleware(
routerMiddleware(history),
thunkMiddleware
)
);
export { store, history };
行动:
export function contentSuccess(retrieveContentWasSuccessful) {
return { type: types.RETRIEVE_CONTENT_SUCCESS, contentSuccess };
}
export function retrieveContent() {
// make async call to api, handle promise, dispatch action when promise is resolved
return function (dispatch) {
return viewContent().then(retrieveContentWasSuccessful => {
dispatch(contentSuccess(retrieveContentWasSuccessful));
}).catch(error => {
throw (error);
});
};
}
减速器:
export function contentReducer(state = fromJS({}), action) {
switch (action.type) {
case types.RETRIEVE_CONTENT_SUCCESS:
return state.merge(action.content)
default:
return state;
}
};
商店本身是这样的:
function mapStateToProps(state) {
return {
content: state.content
}
}
function mapDispatchToProps(dispatch) {
return {
actions: bindActionCreators(thunks, dispatch)
};
}
class ConnectedConnect extends Component {
componentDidMount() {
const { actions } = this.props
actions.retrieveContent();
console.log(this.props.content)
}
render() {
return (
<div>
<Header heading="Demo Heading" colour="orange" disableAnimation={false} />
</div >
);
}
}
`const Content = connect(mapStateToProps, mapDispatchToProps)`(ConnectedConnect);
export default Content
const history = createBrowserHistory()
const store = createStore(
connectRouter(history)(rootReducer),
applyMiddleware(
routerMiddleware(history),
thunkMiddleware
)
);
export { store, history };
api已成功调用。然而,我似乎无法实际访问商店中的响应!感谢您在这方面的帮助!
谢谢 您想在此处传递retrieveContentWasSuccessful而不是ContentSuccessful: