[英]Component not being updated after state is changed in reducer
I would like to display the error to my user. 我想向用户显示错误。 An error is thrown and the error and message are updated in the reducer by the action.
动作将引发错误,并在化简器中更新错误和消息。 But for some reason I can't get the error or message to display for the user.
但是由于某种原因,我无法为用户显示错误或消息。 Is there something wrong with the reducer or mapStateToProps?
reducer或mapStateToProps有问题吗? How does the component know the state has been updated?
组件如何知道状态已更新? I'm not sure how to update that.....
我不确定如何更新。
What am I missing? 我想念什么?
reducer: 减速器:
import {CREATE_VEHICLE,VEHICLE_ERROR,
VEHICLE_FORM_PAGE_SUBMIT,FETCH_VEHICLES_SUCCESS} from '../actions/vehicles';
const initialState={message: null, error: null, vehicle:{}};
export default function vehicleReducer(state=initialState, action) {
console.log("in reducer");
switch(action.type){
case CREATE_VEHICLE:
return [...state, Object.assign({}, action.vehicle, action.message)];
case VEHICLE_ERROR:
return{
...state,
error: action.error,
message: action.message
};
default:
return state;
}
}
actions: 动作:
export const vehicleError = (error, msg) => {
return{
type: VEHICLE_ERROR,
error:error,
message: msg
}
};
export const createVehicle=(vehicle) =>{
console.log("vehicle: ", vehicle);
return (dispatch) => {
return axios.post(`http://localhost:9081/api/bmwvehicle/create`,
vehicle)
.then((response) =>{
if (response.ok){
console.log("success");
dispatch(createVehicleSuccess(response.data))
}}, (error) => {
if (error.response.status == 500){
dispatch(vehicleError(error.message, "Could not add vehicle,
please try again."));
}
}
);
};};
component: 零件:
class Vehicle extends React.Component{
constructor(props){
super(props);
}
submitVehicle(input){
this.props.createVehicle(input);
}
render(){
console.log("the error is: ",this.props.error);
return(
<div>
<AddVehicle submitVehicle=
{this.submitVehicle.bind(this)} />
</div>
)
}
}
const mapStateToProps=(state, ownProps) => {
return{
vehicle: state.vehicle,
message: state.message,
items: state.items,
vehicles: state.vehicles,
error: state.error
}
};
const mapDispatchToProps=(dispatch)=>{
return {
createVehicle: vehicle =>
dispatch(vehicleActions.createVehicle(vehicle))
}
};
export default connect(mapStateToProps, mapDispatchToProps)(Vehicle);
In your CREATE_VEHICLE action, i think you meant to return the state with curly braces instead? 在您的CREATE_VEHICLE动作中,我想您是要用花括号将状态返回?
{...state, Object.assign({}, action.vehicle, action.message)}
In your mapStateToProps, you attempt to access state.vehicles
and state.items
, but it does not exist in your default initial state const initialState={message: null, error: null, vehicle:{}}
. 在您的mapStateToProps中,您尝试访问
state.vehicles
和state.items
,但是在默认的初始状态const initialState={message: null, error: null, vehicle:{}}
,它不存在。
To answer your question about how does the component know the (redux) state has been updated, the component knows because you wrapped it in the Redux HoC called connect() which will notify your component via an update anytime the mapped redux state changes. 为了回答有关组件如何知道(redux)状态已被更新的问题,组件知道了这一信息,因为您将其包装在名为connect()的Redux HoC中,它将在映射的redux状态发生变化时通过更新通知组件。 In your case, the component will be updated on changes to the redux store's state.vehicle, state.message, state.items, state.vehicles and state.error.
在您的情况下,组件将在redux存储的state.vehicle,state.message,state.items,state.vehicles和state.error更改时进行更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.