score:0
Once you know what the prop that you're passing in is called, you can access it like so from within your child component: {props.data.map(item => <span>{item.something}</span>}
const Parent = () => {
return (
<Child data={[{ id: 1, name: 'Jim' }, { id: 2, name: 'Jane ' }]} />
);
}
const Child = (props) => {
return (
<ul>
{props.data.map(item => <li key={item.id}>{item.name}</li>)}
</ul>
);
}
score:0
You are passing dataProp down to ChilAccordian as a prop. So in Child component you should access it using props.dataProp and do map on props.dataProp but not on props directly
ChildAccordian:
<h3> Details:
{ Array.isArray(props.dataProp) && props.dataProp.length > 0 ?
props.dataProp.map(d =>{
return <span key={d.id}>{d.key}</span>
})
:
""
}
</h3>
Also keep in mind that you have to add unique key to parent Jsx element when you generate them in loop like for loop, .map, .forEach, Object.keys, OBject.entries, Object.values etc like I did in the above example. If you don’t get unique id from the data then consider adding index as unique like
<h3> Details:
{ Array.isArray(props.dataProp) && props.dataProp.length > 0 ?
props.dataProp.map((d, index) =>{
return <span key={"Key-"+index}>{d.key}</span>
})
:
""
}
</h3>
Edit: If it is an object then do something like below and regarding using a method to generate jsx elements
getMappedData = dataProp =>{
if(props.dataProp){
Object.keys(props.dataProp).map(key =>{
return <span key={"Key-"+key}>{props.dataProp[key]}</span>
});
}else{
return "";
}
}
<h3> Details:
{this.getMappedData(props.dataProp)}
</h3>
Source: stackoverflow.com
Related Query
- React JS - How to access props in a child component which is passed from a parent component
- How is an argument for a callback in React being passed from the child component to the parent component?
- React TS - How to pass props from a parent component to a deeply nested child
- How to get parent props from child composition component React
- How do I access some data of a property I passed from parent component to child component?
- how does react props merge state passed from parent component and redux store
- How to access data in a child react component from a parent react component
- ReactJS - how to fetch inside a child component with details passed on by props from a parent component?
- How to access props from Parent Component inside the Child Class Component ? (specifically for componentDidMount)?
- How to mock a parent component which passes props to child component using react testing library
- How to stop React child component from loading before data from fetch call in parent is passed to it
- How do I access multiple state in parent from child component in React Native
- How to set state in parent from one child component and access the state in another child component using react and typescript?
- React JS : How To Access Child Component's State Objects From Parent Component
- how to pass props from one component to other in react router that don't have parent child relationship?
- react (class based) : How can I update JSON object in parent component using setState after the object being passed in function from child to parent?
- How to use onClick event to pass the props from child component to parent component React Hooks
- React child component does not re-render when props passed in from parent changes
- How to pass function as props from functional parent component to child
- How to pass input value from child to parent component react
- Access child state of child from parent component in react
- How to communicate from Child Component to Parent Component with React Router
- Updating Child Component Via Change of Props from Parent in React
- How to update parent state from child component in React + send a paramater
- React child component not rendering props from parent value
- How test callback click in child component that was passed from parent component?
- How to pass an Array from Child component to Parent component in react I know how to pass from parent t
- How can I access child components values from a parent component when they are added dynamically?
- How to trigger an event in Parent Component from Child Component onClick() using React Hooks?
- How to pass form values from child component to parent in react
More Query from same tag
- React Redux object automatically sorts array
- Not sure how to map and display data in react app
- How can i export as multiple words in reactJS?
- Missing or unused dependency array in useEffect
- React - accessing selected option on button click?
- React - how call function parent from grandchild
- Firebase Web - Signout - Cannot delete property '0' of [object Array]
- How do I change the width of the modal? its too small. react-responsive-modal
- Appending React States to another State
- Is it good to store all the fetched data from api call in redux store?
- React) How do I ref to the same component (child)?
- React-flow line width
- Difference between elevation and z-index in material-ui?
- static class property not working with Babel
- How to test hidden Text which has a toggle function by using testing-react-library?
- yarn install failing import sys; print "%s.%s.%s" % sys.version_info[:3] invalid syntax
- React not rerendering when the state is an empty object
- If else different onclick functions react?
- How target DOM with react useRef in map
- Can't access object property even though should be there
- jest, enzyme - testing a method that returns jsx
- How to create a counter that calculates the remaining characters?
- Can I make "each child in an array or iterator should have a unique "key" prop" warning into an error?
- How to call parent function from custom child component?
- Trouble while using .svg file in React.js code
- React coreUI submit values not getting captured
- Page is rendering but not components although everything compiles and styles can be added outside the component
- React onClick function doesn't fire
- Route change before action creator completes
- How to toggle class of an element with ReactJs