Blog>
Snippets

Initializing State with Object.create(null)

Demonstrate initializing the Redux state using Object.create(null) to create a truly empty object without prototype.
const initialState = Object.create(null);
console.log('Prototype of initialState:', Object.getPrototypeOf(initialState));
// This should log 'null', showing that initialState has no prototype.
Create a truly empty object without a prototype for the Redux initial state using Object.create(null).
function rootReducer(state = initialState, action) {
  // Your reducer logic here
  return state;
}
Defines a root reducer with the initialState that does not have a prototype. This is where you would handle actions and update the state accordingly.
const store = Redux.createStore(rootReducer);
console.log('Initial state:', store.getState());
// This should log the initialState which is an empty object without a prototype.
Creates a Redux store with the rootReducer. The initial state of the store will be the truly empty object we created.
<!DOCTYPE html>
<html>
<head>
  <title>Redux Example</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.5/redux.min.js"></script>
</head>
<body>
  <script type="text/javascript">
    // The JavaScript code from above goes here
  </script>
</body>
</html>
HTML structure to include the Redux library via CDN and a script tag where the Redux-related JavaScript code will be placed.