动作/ index.js
export const fetchAppointment = (userId) => async dispatch =>{
const request = await axios.get(`${URL}/apis/appointments/${userId}`)
dispatch({type :types.FETCH_APPOINTMENT, payload:request.data})
};
减速器/ reducer_appointment.js
import _ from 'lodash';
import * as types from '../actions/types';
export default function(state = null, action) {
switch (action.type) {
case types.FETCH_APPOINTMENT:
return _.mapKeys(action.payload, 'patients_id') || false
default:
return state;
}
}
app.js
renderAppointment(){
console.log(this.props.appointment)
switch(this.props.appointment){
case null:
console.log("null case")
return;
case false:
console.log("false case")
return <div>false case</div>;
default:
console.log("default case")
return <div>default case </div>;
}
}
问题我总是得到默认情况虽然我得到数据或没有数据。我希望虚假案例在没有数据的情况下有效。
更新
我检查了结果,_。mapKeys返回undefined。所以我想知道并发布这篇文章!
请帮助我!
答案 0 :(得分:7)
我猜是因为_.mapKeys
总是至少返回一个空对象。因此,即使有效负载为{}
,您的状态仍为undefined
。