score:1
You seem to be missing a return
statement in the loop
method.
You can cascade rendering as deep as you'd wish, only remember that you need to call the method instead of just placing it in the component structure (see this.loop
without call parentheses in your sample):
var myComponent = React.createClass({
renderListElements: function (parent) {
return this.state.listElements[parent].map((element, index) => (
<li
className="my-component__sub-component__list-element"
key={`${parent.uid}_${element.uid}`}
>
{element.name}
</li>
));
},
render: function () {
var parentsId = [ 0, 1, 2, 3 ];
return (
<div className="my-component">
{parentsId.map((item, index) => (
<div
className="my-component__sub-component"
key={item.uid}
>
{this.renderListElements(item)}
</div>
)}
<div/>
);
}
});
score:3
How about this way :
loop: function(ingredients) {
return ingredients.map((ingredient, index) => {
return (<li key={index} className="list-group-item">{ingredient}</li>)
})
},
render(){
...
{this.loop(item.ingredients)}
...
}
One more thing, you shouldn't use index of array as key
because it will be difficult to manage when editting the array later. It will be better if you assign key
with something very unique like id
or index + Date.now()
Source: stackoverflow.com
Related Query
- How to loop through properties of objects inside of an array within a React component?
- How do I loop through an array in an array of objects
- How to loop through an array an put create an array of objects using react and javascript?
- How to loop through an array of objects and get specific key values in React?
- How to loop through an array of array of two objects Alphavantage API
- How do i loop through an array of objects in react
- How to recursively loop through array of objects and find the index of matching id using javascript and react?
- How do I create an array of objects in a React class component through a for loop to be mapped in a functional component?
- How to loop and render elements in React.js without an array of objects to map?
- Loop through simple array of objects in React
- How to loop through an array of data in React-native?
- React Object of Objects - How to loop through
- how to properly iterate through array of post objects retrieved through Wp Rest Api in react js
- How to loop through objects in JSX react
- How to iterate through an array of objects from json in React.js
- How to map through nested array of objects in React
- In Javascript, replace forEach that involves filtering the same array of objects each time through loop with a left join
- How I can loop through the similar objects in state and push them into object?
- How can I filter through an array of objects based on a key in a nested array of objects?
- In React.js, how can I loop through an array of jsx elements and add attributes to them
- Fetching the input value from textarea in a loop from an array of objects using reactjs. How to do so?
- loop through array of objects using ramda
- How to loop through an array of strings backwards x amount of times and start over when the end is reached?
- How do I loop through an array in javascript to get a new array whose length is not greater than 4?
- How to iterate through array of objects and execute a request using react?
- how to loop through two array in react?
- How to render/map through an array of objects in React
- How do I map an through an array of objects and display each object individually? I want each pizza order to show
- How can loop through array of JSON Object to find property value that includes user input characters
- How to loop through complex object or array of object
More Query from same tag
- ReactJS: Page update after login
- Testing Typescript Interface with Jest
- Vercel Next.js build error while prerendering pages
- Remove Y Axis line but keep the values in recharts
- react-virtualized, SystemJS and CDN
- WebSocket connection to "wss" sockjs-node failed for CRA and nginx reverse proxy
- D3.js with React - Can not see Lines I'm drawing
- Styled-components and theming with NextJS
- Having to apply interface twice with forwardRef Typescript definition
- `window.location.href` does not work on react app when deploying on github pages
- Axios not POSTing array
- Changing colors of antdesign header
- How to substitute array value in React.js?
- How to dispatch actions via a timer using Redux
- Sending state data down to parent component
- My react app is giving an error of "action undefined"
- Am I using the useEffect hook correctly to try and fetch data?
- How to get a config value into a href as string instead of object object
- Registering Users in React
- React warning render()
- How to resolve cannot read property 'prototype' of undefined
- Error [ERR_HTTP_HEADERS_SENT]: Cannot remove headers after they are sent to the client
- How to import ReactCSSTransitionGroup using es6 syntax?
- Progress circle bar in React JS
- TypeScript: having trouble typing useReducer with generics
- Redux mapStatmentToProps returns undefined
- How Do I Deploy Flask API & React Frontend Microservices to Google App Engine with Proxy?
- Nothing is Displaying on my browser after running npm start in react app
- Vis.js/React/JavaScript: Render Vis.timeline
- Casperjs google auth popup