3

I am not sure if this is common practice, but while I was going through the highly popular react-boilerplate app I noticed that the default value of the store keys were always getting set to false regardless of whether the actual type is Array, Object, Number or String.

// The initial state of the App
const initialState = fromJS({
  loading: false,
  error: false,
  currentUser: false,    // String
  userData: {
    repositories: false, // Array of Objects
  },
});

function appReducer(state = initialState, action) {
  .....

Is there a best practice in play here? Because in this implementation the data type in the JSX component will need to be Array or Boolean instead of just Array which is what the type should be in the first place.

repos: PropTypes.oneOfType([PropTypes.array, PropTypes.bool]),

Thank you in advance.

nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • 1
    I am sure, that this is not the best practice. You should always initialize the redux states to its appropriate data type. I don't have anything to prove it, but it will be simpler and performant according to my idea of how javascript works. – Vishal Aug 24 '18 at 20:16
  • 1
    It doesn't look like a good practice to me. – Anas Aug 24 '18 at 20:16
  • 1
    Maybe you should open an issue in the repo asking that. It doesn't look like a good practice to me neither. – Facundo Matteo Aug 24 '18 at 20:17

1 Answers1

0

This is not going to be always Array/Boolean. When we store API response in redux, It usually comes in JSON format so it totally depends about data type.

Take this as ane example:

const INITIAL_STATE = { 
  isSearchDestinationAvailable: false,
  searchedDestination: {},
  destination: []
};

If you changes any of their data type while initializing the state, your app might crash while validating the updated props unmounting/clearing redux store. I have had a great time with them.

Feel free to ask any further query.

Sakhi Mansoor
  • 7,832
  • 5
  • 22
  • 37
  • In this scenario, we know for a fact that the `repos` data is going to be an array, as it is based on the GitHub API. According to the store design, the data type will only be Boolean or Array because we're _setting_ it as such. I didn't quite understand your explanation – nikjohn Aug 24 '18 at 20:58
  • There might be a case when you are passing props down to a children which are coming from redux and there is some conditional rendering or statements you need to validate. You need to have proper data types otherwise there is a probability a unintended code block would be executed. – Sakhi Mansoor Aug 24 '18 at 21:11
  • 1
    Okay - so in your opinion, the approach of giving the initial state is good or bad? What you have mentioned seems to be dealing with how to define the `PropTypes` in the `JSX` file than with how to initialize the Redux state – nikjohn Aug 24 '18 at 21:34
  • yes @nikjohn It is a must thing to intialize redux state but with appropriate data type that you're expecting. You really need to be careful when it comes nested object initial state. This would be more clear when we actually go through the situations. I hope you've got the point already. – Sakhi Mansoor Aug 24 '18 at 21:37