【发布时间】:2019-03-11 18:07:07
【问题描述】:
嗨,当我在控制台记录我的组件道具(从 redux 传递下来)时,我得到的初始状态为空。但是使用反应检查器我得到了 axios 请求的结果。我尝试阅读几十个类似的问题,但无法解决我的问题。
操作
import { searchService } from '../api/searchService';
export const actions = {
FETCH_USERS: 'FETCH_USERS',
}
export const searchUsers = () => dispatch => {
searchService.get('/search')
.then((result) => {
dispatch({
type: actions.FETCH_USERS,
payload: result
})
})
}
减速器
import { actions } from '../actions';
export default (state = null, action) => {
switch(action.type) {
case actions.FETCH_USERS:
return action.payload;
default:
return state;
}
}
搜索组件
function mapStateToProps ({search}) {
return {search};
}
const mapDispatchToProps = dispatch => ({
searchUsers: () => dispatch(searchUsers())
});
export default connect(mapStateToProps, mapDispatchToProps)(withAuth()(Search));
【问题讨论】:
标签: reactjs redux axios redux-thunk