score:0
Accepted answer
you can combine all the arrays and do the map in one step like this
const cardlist = ({ results0, results1, results2, results3, results4 }) => {
return (
<fragment>
{[...results0, ...results1, ...results2, ...results3, ...results4].map((result, i) =>
(<card
key={i}
name={result.name}
skin_color={result.skin_color}
eye_color={result.eye_color}
birth_year={result.birth_year}
gender={rresult.gender}
/>))
}
</fragment>
);
};
export default cardlist;
score:0
first, if the callbacks are identical, factor them out into a utility function, remember to use the current value of the array::map callback
const renderperson = (person, i) => (
<card
key={i}
name={person.name}
skin_color={person.skin_color}
eye_color={person.eye_color}
birth_year={person.birth_year}
gender={person.gender}
/>
);
this can be further reduced using object destructuring
const renderperson = ({ birth_year, eye_color, gender, name, skin_color }, i) => (
<card
key={i}
name={name}
skin_color={skin_color}
eye_color={eye_color}
birth_year={birth_year}
gender={gender}
/>
);
now you've reduced your code to a more manageable
results0.map(renderperson)
results1.map(renderperson)
results2.map(renderperson)
results3.map(renderperson)
results4.map(renderperson)
but you've still some repetition, you can dump the 5 result props into an array and also map them
[results0, results1, results2, results3, results4].map((result, i) => (
<fragment key={i}>
{result.map(renderperson)}
</fragment>
))
Source: stackoverflow.com
Related Query
- How to refactor a code redundancy in the map method using React.js
- How to add items into specific Id using the react array map method
- How to refactor the ternary operator code using react and typescript?
- How to refactor <List> contents using the map() method in React JSX?
- how to write inline js event handler code in the react component using "this"
- How to properly make a GET call in React returning an observable (resembling the method in Angular and not using promises)?
- How can i get the location of the user - Using at React Map GL
- React - How to change code written in the old way when using a new approach?
- How to add a legend to the map using react leaflet, without using refs and manually modifying the DOM?
- How to execute axios code using the window's beforeunload event in React
- How to display a fixed code snippet on the website using react
- React - How to select all checkboxes based on one without using the map function
- How to modify the code such that can call usehook using react and typescript?
- How to add if statement in the map function using react
- How to style the React google map marker using the given image file
- How to fix the count variable value in a recursive method using react and javascript?
- How to add an image in the first react component and ignore in rest of the components using map function
- How can I comment out some HTML code defined into the return() method of a React component?
- How to refactor code using styled components in react and typescript?
- How do I increment the count of a selected index of state object using setState method given by useState hook in my react application
- How can I implement the filter method to this code without deleting map method?
- How do I show the user the specific http response code & message using React Hooks
- How to implement Drop down in React JS using Array for the below snippet of code
- How to refactor the if else code using typescript and react?
- How to manipulate the DOM inside of map function using onChange event on one of the checkboxes with react hooks
- How to prevent triggered all forms inside a map at the same time using react hook form?
- How to select a single list element when using map in React to alter the state value
- How to fix map is not function even there is already set data in the useState using React Hook
- How to add checkbox or radio button inside the map method in react component?
- How to call array data on button onclick using map method in React JS
More Query from same tag
- React: How to get a group value of components?
- create-react-app --typescript: Cannot compile namespaces when the '--isolatedModules' flag is provided
- React Redux how to dispatch using ownProps route parameter
- React Redux mapStateToProps is re-rendering
- Getting a warning in console "You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before."."
- How do I do nested routers?
- POST request returns 401 during cross origin preflight
- How to compare array values with object properties in this example?
- React 16.9.0 "javascript:;" href alternative?
- Jest/Enzyme TypeError: (0 , _store.configureStore) is not a function
- Replace setTimeout in useEffect when waiting for props
- Unable to start React with TypeScript in Docker container
- React doesn't update component state when user changes input
- react helper import fails in production but works in development
- Why is this png being pushed to the left?
- Dispatch action on Auth0's lock.on('authenticated') event
- Do react query result objects go stale due to closures?
- Uncaught ReferenceError: __VITE_IS_MODERN__ is not defined
- Day.js countdown timer
- convert object array to react component
- Is it possible to reduce the image size when converting it into base64 format in react js?
- Redux-thunk dispatch not working
- In React, how do I name a field of my form that is part of an array?
- React hooks: Set state 'ModalVisible' but state does not update immediately
- How to scroll to a component from another component in React?
- React assign dynamic id in setState?
- React.cloneElement memory efficiency
- Name webpack chunks from react-loadable
- How to build a route on yandex maps in react via react-yandex-maps
- React - Manage Bootstrap Modals in components