/** * Sets a specific property of a specific state to a specific value. Prevents * unnecessary state changes (when the specified value is equal to the * value of the specified property of the specified state). * * @param {Object} state - The (Redux) state from which a new state is to be * constructed by setting the specified property to the specified * value. * @param {string} property - The property of state which is to be * assigned the specified value (in the new state). * @param {*} value - The value to assign to the specified property. * @returns {Object} The specified state if the value of the specified * property equals the specified value/tt>; otherwise, a new state * constructed from the specified state by setting the specified * property to the specified value. */ export function setStateProperty(state, property, value) { // Delete state properties that are to be set to undefined. (It is a matter // of personal preference, mostly.) if (typeof value === 'undefined' && Object.prototype.hasOwnProperty.call(state, property)) { const newState = { ...state }; if (delete newState[property]) { return newState; } } if (state[property] !== value) { return { ...state, [property]: value }; } return state; }