Blog>
Snippets

Action Creators with Object.create(null) Objects

Create action creators that return actions with type and payload properties, using Object.create(null) to ensure no inherited properties.
function createAction(type, payload) {
  // Create an action with no inherited properties
  let action = Object.create(null);
  action.type = type;
  action.payload = payload;
  return action;
}

// Example usage:
// const myAction = createAction('MY_ACTION_TYPE', { key: 'value' });
This function creates an action object with a type and payload property. It uses Object.create(null) to ensure the object does not inherit any properties from Object.prototype.
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Action Creators Example</title>
</head>
<body>
    <script src="actionCreators.js"></script>
</body>
</html>
This HTML file includes a basic structure and links to a JavaScript file named actionCreators.js, where the createAction function is defined.