简体   繁体   English

在reducer中更改状态后未更新组件

[英]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.vehiclesstate.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.

相关问题 在状态由另一个reducer更新后运行reducer - Run reducer after state is updated by another reducer React TypeScript-更改父状态时不更新组件属性 - React TypeScript - Component properties not being updated when parent state is changed 在减速器中更改状态后未调用 MapStateToProps - MapStateToProps not being called after changing STATE in reducer 在React类组件中,为什么第一次单击按钮时状态没有改变? 但是在第二种情况下,它会更新吗? - In react class component why is the state not being changed in the first instance of button click? But in the second instance it gets updated? 是否可以在 state 更改并使用后更改减速器的初始 state 并使用它? 反应 - is it possible to change the initial state of reducer after state changed and use it ? React 减速器未返回更新状态 - Reducer not returning updated state Redux-在reducer中更新状态,但不更新组件的新值 - Redux - state updated in reducer but not updating the new values to component 减速器未更新 - Reducer's not being updated 状态更改后,React-Redux连接的组件不会更新 - React-Redux connected component doesn't get updated after state is changed 从减速器分派异步调用后无法获得更新的状态 - Unable to get updated state after dispatching an async call from reducer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM