title: Remove duplicate code in the reducer in react
body: <p>I have duplicate code in reducers. Where only the name of the property changes. How can I refactor to remove duplicates?</p>
  <pre><code>const reducer = (state, action) =&gt; {
  switch (action.type) {
      case ADD_ITEM: {
          if (state.toggleCost) {
              let last = state.costs.length
              return {
                  ...state,
                  costs: [...state.costs, { name: action.propertyName, value: action.value, id: last + 1 }
                  ],
              }
          } else {
              let last2 = state.profit.length
              return {
                  ...state,
                  profit: [...state.profit, { name: action.propertyName, value: action.value, id: last2 + 1 }]
              }
  
          }
      }
  </code></pre>