嗨,当我在控制台上登录组件props(从redux传递下来)时,我得到的初始状态为null。但是使用反应检查器我得到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));
答案 0 :(得分:0)
您的问题出在减速器中 首先,您应该设置一个初始状态,然后需要编辑此状态,以使Redux感觉到更改并更新
检查下面的代码,让我知道它是否对您有用。
import { actions } from '../actions';
const INITIAL_STATE= {search: ""};
export default (state = INITIAL_STATE, action) => {
switch(action.type) {
case actions.FETCH_USERS:
return {...state, search: action.payload};
default:
return state;
}
}