Why are default states on redux initialized to false instead of empties?

Viewed 565

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.

1 Answers

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.

Related