0

I have set up my very first Redux projet and I am trying to create a store for my current user by fetching it from my rails backend.

Although everything seems fine, this.props.user gives null in the component.

store.js:

import { createStore, applyMiddleware } from "redux";
import thunk from 'redux-thunk';
import rootReducer from "./reducers";

const store = createStore(
  rootReducer,
  applyMiddleware(thunk)
);

export default store;

actions.js

import { CREATE_USER, PROPAGATE_LOGIN, PROPAGATE_LOGOUT } from "./actionTypes";
import axios from 'axios';

export const getCurrentUser = () => {
  return dispatch => {
    axios.get("/users/get_current_user", {})
    .then(response => {
      if (response.data.user) {
        dispatch(propagateLogin(response.data.user));
      } else {
         console.log("pas d'utilisateur connecté.")
         dispatch(propagateLogout());
       }
    });
  };
};

export const propagateLogin = (user) => ({
  type: PROPAGATE_LOGIN,
  payload: {
    user
  }
});

export const propagateLogout = () => ({
  type: PROPAGATE_LOGOUT,
  payload: { }
});

users.js reducer:

import { CREATE_USER, PROPAGATE_LOGIN, PROPAGATE_LOGOUT } from "../actionTypes";

const initialState = {
  user: null
};

export default function(state = initialState, action) {
  switch (action.type) {
    case PROPAGATE_LOGIN: {
      return {
        user: action.payload.user
      }
    }
    case PROPAGATE_LOGOUT: {
      return {
        user: null
      }
    }
    default:
      return state;
  }
}

AppRouter.js (the connected component):

class AppRouter extends React.Component  {
 defaultState() {
  return {
    isReady: false,
    user: this.props.user,
    loginModalOpen: false,
    signupModalOpen: false
  }
}

constructor(props) {
  super(props)
  this.state = this.defaultState()
}

componentDidMount() {
  this.getUser();
}

getUser(history = undefined) {
  this.props.getCurrentUser();
  this.setState({
    isReady: true
  });
}

render (){
// [...]
}

};

const mapStateToProps = (state /*, ownProps*/) => {
  return {
    user: state.users.user
  }
}

const mapDispatchToProps = { getCurrentUser, propagateLogout }

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

And here is a screenshot from the React dev console: for the Provider Component:

enter image description here

1 Answer 1

1

As you write in user.js reducer user inital state is null

const initialState = {
  user: null
};

Since get user action is async, you are just assining null value to user in inital state

 defaultState() {
  return {
    isReady: false,
    user: this.props.user, //this.props.user null here
    loginModalOpen: false,
    signupModalOpen: false
  }

You can use this.props.user without assing it state value

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.