React Redux state changes


illusory

this.props.authStateremains the same, although I dispatch an action in the componentDidMountfunction :

componentDidMount() {
      if (localStorage.getItem('token')) {
        dispatch(updateAuthState('AUTHENTICATED'));
      }
  }

render() {
 <div>{this.props.authState}</div>
}

Home.propTypes = {
    authState: PropTypes.string
};

const mapStateToProps = (state) => {
    return {
        authState: state.authState
    }
};

const mapDispatchToProps = (dispatch) => {
  return {

  }
};

export default connect(mapStateToProps, mapDispatchToProps)(Home);

The output is NO_AUTH (initial value of authState)

reducer:

export function authState(state = "NO_AUTH", action) {
    switch (action.type) {
        case 'AUTH_STATE':
            return action.authState;
        default:
            return state;
    }
}

Any idea why?

Bhojendra Rauniyar

You are currently dispatching directly inside componentDidMount which is not mapped to:

connect(mapStateToProps, mapDispatchToProps)(Home);

This should do the job:

componentDidMount() {
      if (localStorage.getItem('token')) {
        this.props.onUpdateAuthState('AUTHENTICATED');
      }
  }

const mapDispatchToProps = (dispatch) => {
  return {
    onUpdateAuthState: function(authState) {
      dispatch(updateAuthState(authState));
    }
  }
};

Now this will get the authState:

const mapStateToProps = (state) => {
    return {
        authState: state.authState
    }
};

Related


React Native with Redux: state changes not showing in console

Eliot When I put statements console.log('test')in the reducer, I can see them in the console when the action is called. But I don't see the redux "NEXT STATE"/"PREV STATE" stuff in the console. Is there any basic knowledge I might be missing? In the code below

React doesn't update views even when Redux state changes

Ajay The problem is that React doesn't run the render function when I update the state in Redux. I'm a beginner with Redux so I can't figure out exactly what to do with it. I read about the @connect function, but when I use the CreateReactApp CLI tool, I can't

React Redux store state changes not working

John Du The super simple code below is not working. I'm using React and injecting/providing props through a redux store. My understanding is to store injected props in React components. Why do I need both line1 and line 2 in order for this component to work? i

React/Redux - Create XHR requests based on state changes

Bryce I have a method called from a button click. In this method I toggle some state between true or false. After switching the state, I need to make an XHR request to the server and use the state I just changed. handleButtonClick = () => { //toggle 'in_pr

Component doesn't render based on state changes in react-redux

Sudarshan When mapStateToProps is updated and the props are not accepted in the item component, the item is not rendered; if a CombineReducer is used, the item is valid, but without a CombineReducer, the item is not rendered. reducer.js This is my reducer, I

React Native with Redux: state changes not showing in console

Eliot When I put statements console.log('test')in the reducer, I can see them in the console when the action is called. But I don't see the redux "NEXT STATE"/"PREV STATE" stuff in the console. Is there any basic knowledge I might be missing? In the code below

React Native with Redux: state changes not showing in console

Eliot When I put statements console.log('test')in the reducer, I can see them in the console when the action is called. But I don't see the redux "NEXT STATE"/"PREV STATE" stuff in the console. Is there any basic knowledge I might be missing? In the code below

React-redux initialization function regardless of state changes

Bukovski I really don't know how to describe it as precisely as possible. After the async call, I need some sort of initialization function that can only be called once, whether or not the state is changed later. I have a component, when it mounts, I use an ac

React Redux store state changes not working

John Du The super simple code below is not working. I'm using React and injecting/providing props through a redux store. My understanding is to store injected props in React components. Why do I need both line1 and line 2 in order for this component to work? i

React doesn't update views even when Redux state changes

Ajay The problem is that React doesn't run the render function when I update the state in Redux. I'm a beginner with Redux so I can't figure out exactly what to do with it. I read about the @connect function, but when I use the CreateReactApp CLI tool, I can't

Props not updating when redux state in React Hooks changes

Ertan Hasani I'm trying to implement Redux on a React Hooks project, but it doesn't seem to be working very well. Am I doing something wrong here? reducer.js const initialState = { educations: [] }; export default function home(state = initialState, actio

React components don't update with Redux state changes

have I have cart data in this form const cart = { '1': { id: '1', image: '/rice.jpg', price: 32, product: 'Yellow Corn', quantity: 2, }, '2': { id: '2', image: '/rice.jpg', price: 400, product: 'Beans', quantity: 5

React Redux state changes

illusory this.props.authStateremains the same, although I dispatch an action in the componentDidMountfunction : componentDidMount() { if (localStorage.getItem('token')) { dispatch(updateAuthState('AUTHENTICATED')); } } render() { <div>{

React reacts to state changes differently than Redux

Gerriott I've been scratching my brain with this for a while now, any help would be greatly appreciated. I'm using React with the Redux Toolkit and I'm having trouble getting React to remove "todos" from my UI even though Redux responds as expected. In Redux D

React Native with Redux: state changes not showing in console

Eliot When I put statements console.log('test')in the reducer, I can see them in the console when the action is called. But I don't see the redux "NEXT STATE"/"PREV STATE" stuff in the console. Is there any basic knowledge I might be missing? In the code below

React Native with Redux: state changes not showing in console

Eliot When I put statements console.log('test')in the reducer, I can see them in the console when the action is called. But I don't see the redux "NEXT STATE"/"PREV STATE" stuff in the console. Is there any basic knowledge I might be missing? In the code below

React Redux state changes

illusory this.props.authStateremains the same, although I dispatch an action in the componentDidMountfunction : componentDidMount() { if (localStorage.getItem('token')) { dispatch(updateAuthState('AUTHENTICATED')); } } render() { <div>{

React Redux store state changes not working

John Du The super simple code below is not working. I'm using React and injecting/providing props through a redux store. My understanding is to store injected props in React components. Why do I need both line1 and line 2 in order for this component to work? i

React Redux store state changes not working

John Du The super simple code below is not working. I'm using React and injecting/providing props through a redux store. My understanding is to store injected props in React components. Why do I need both line1 and line 2 in order for this component to work? i