score:3
Accepted answer
i believe that for you to take out the conditional statement, assuming that the name of the key of the order is the same as the service name from your payload, you can access and assign the key by passing the value from the payload itself. so instead of something like you have in the reducer, you will get something like this:
const addtocart = (state, action) => {
return {
...state,
order: {
...state.order,
[action.payload.service]: [...state.order[action.payload.service], action.payload.item]
}
}
}
Source: stackoverflow.com
Related Query
- Refactoring javascript code react redux issue
- Explanation this JavaScript React Redux Code
- React - Redux App: "Invalid attempt to spread non-iterable instance" Issue
- Jest "No tests found, exiting with code 1" error on Windows 10 in React Redux application
- why vscode is not recognizing react code as react javascript instead it format it as vanilla javascript
- Best solution to share code between 2 separate projects React Js and React Native? ( like redux store)
- There is TypeError (0 , _ColorReducer.color) is not a function error in React + Redux code
- How to load a react component written in JSX code from my Javascript code
- why is React source code written in JavaScript instead of TypeScript, but still have types?
- React Bootstrap Typeahead AsyncTypehead synchronous issue with Redux
- React / Javascript Out Of Order Code Execution
- Converting JavaScript code to react component class format
- Having an issue while up and running unit test with react redux testing library
- React Redux Refresh issue
- React Redux - quick flash of previous state before dispatch issue
- What I need to write in these places in code in Redux app(remake pure react to redux)?
- React with Redux Axios get request error issue with laravel 5.2 api server
- Issue with React Redux array.push
- How to write JavaScript code inside html attributes in React JS
- React JavaScript with ES6 compiling issue under browserify and babel
- React - reusable Redux hook slice issue (Redux Toolkit, Typescript)
- Error: 'import' and 'export' may only appear at the top level when I reworked small part of REACT code to REDUX
- react redux async load FB javascript sdk
- React Javascript - setState issue and copyToClipboard issue with scaleable button+p render
- React Redux - Issue removing an item
- Mapping through an array using react Hooks - REFACTORING CODE
- React Redux Form (Multiple Forms) - Routing & State Handling Issue
- Converting a javascript map code to React
- Getting status code 304 on a get request with axios using react and redux
- Calculate lines of text in javascript in React with Hooks and redux
More Query from same tag
- Import ReactDOM undefined,
- how to scrolled according to content using react js?
- How to integrate React-app-rewired with Customize-CRA
- Why are my React form submission values undefined?
- How to communicate between Mobx stores created with React.createContext
- Can't stop an infinite loop at nested component at useEffect hook
- Are these two JavaScript statements equivalent under functional programming rules in React
- Reducer/Context Api
- google map react, from onGoogleApiLoaded apiIsLoaded doesn't called
- React ES6 onSubmit() does not work
- React hooks useEffect running even with [] given as deps
- How to make second GraphQL mutation after first one is completed in loop
- how can i send an api call from the supreme function to another component called product to change a mock data called cart: true and display in cart
- How to display true for a certain range of digits?
- Unable to use React Context in deep descendant components
- Calculating quantity and price total in react
- http-proxy-middleware not proxying to index page of other server?
- React router location only updated in some components
- react-router-dom Link on click close bootstrap modal window
- Unable to overwrite local variable in Typescript
- Can you pass props to a JSX reference?
- React Bootstrap Tooltip is not working on full screen
- How can I pass the index from javascript map function to the onclick function?
- Infinite loop with nested object
- React js says Cannot read property 'name' of undefined when calling json files
- React-Router v6 confusion
- typescript doesn't render dynamic url on reac-router-dom
- How to prevent inner div's event handler from executing in outer div's event handler?
- TypeError: Cannot read property 'classList' of undefined with Image-Slider
- Basic React code not producing any output