Data can be passed to components via props.
https://facebook.github.io/react/tutorial/tutorial.html#passing-data-through-props
In your case props would be accessed inside the components via this.props.
<TodoList /> takes a prop called items which is an array. Inside <TodoList /> you can map through that array and return elements.
For example in the render method of your class you would return TodoList with a prop of items:
const myItems = [{ name: 'item 1' }, { name: 'item2' }];
function MyApp() {
return (
<TodoList items={myItems} />
);
}
Then in TodoList you map the items
function TodoList({ items }) {
return items.map(item => (
<h1>{item.name}</h1>
));
}
Answer from pizzarob on Stack OverflowData can be passed to components via props.
https://facebook.github.io/react/tutorial/tutorial.html#passing-data-through-props
In your case props would be accessed inside the components via this.props.
<TodoList /> takes a prop called items which is an array. Inside <TodoList /> you can map through that array and return elements.
For example in the render method of your class you would return TodoList with a prop of items:
const myItems = [{ name: 'item 1' }, { name: 'item2' }];
function MyApp() {
return (
<TodoList items={myItems} />
);
}
Then in TodoList you map the items
function TodoList({ items }) {
return items.map(item => (
<h1>{item.name}</h1>
));
}
You just set up an attribute, with the same name
<ToDoList items={myitems} />
Syntax to dynamically pass an array to component
Passing arrays to react component - Solved
Can't pass array as props to child functions [beginner]
Passing array of objects to function adds to array
Videos
Hi Everyone,
This feels like a 101 level question, but its been a long day!
In App I am importing a bunch of arrays all with the naming convention xxxxArray. Eg raceArray, roleArray
App also has a state "choice" and child buttons that update "choice" to a string. Eg "race", "role."
App also calls another component that should take an imported array as props - eg <Details collection={raceArray} />
For the life of me I cannot workout how to take the "choice" string and concatenate it with "Array" to have it refer to one of the imported arrays that is then passed to <Details>.
This seems like a pretty quick thing - am I thinking about it wrong? Any advice appreciated
Hello!
I'm taking my first steps into React -and JV- and encounter a problem I can't solve. It looks like one of those that are actually incredibly simple, the answer just in front of my eyes. But, dammit, I can't see it.
With create-react-app I've been messing with the App.js file. I'm trying to define an Array of objects, and then pass those objects to child functions.
App function:
import React from 'react'
const App = () => {
const parts = [
{
name: 'This is part 1',
exercises: 20
},
{
name: 'Part two it is',
exercises: 5
},
{
name: 'Threeee',
exercises: 16
}
]
console.log(parts) //output array: (3) [{…}, {…}, {…}]
console.log(parts[0]) //output object:{name: "This is part 1", exercises: 20}
return (
<div>
<Content parts={parts} />
</div>
)
}Content function:
const Content = (props) => {
console.log(props) //output object!?: {parts: Array(3)}
console.log(props[0]) //output: undefined
return (
<>
<Part1 props={props[0]} />
<Part2 props={props[1]} />
<Part3 props={props[2]} />
</>
)
}part1 function (part2 and part3 are the same as this):
const Part1 = (props2) => {
console.log(props2) //output object!?: {props: undefined}
return (
<>
<p>
{props2.name} {props2.exercises}
</p>
</>
)
}What I need to accomplish for the exercise is to only pass the array parts in App function like this: <Content parts={parts} />
Then Content function should use the object indexed 0 on the array and pass it to Part1, which should return the <p> code.
I guess there's an error when passing the array to Content. It gets converted to an object and then I cant use the index method. If I try props[0] I get nothing for it is undefined. If I try to console.log(props[0].name) I get the error: TypeError: Cannot read property 'name' of undefined
How can I pass the array to the child functions? I know the structure of the app might not be ideal, but it's exercise 1.4 for the Fullstackopen Course, by the University of Helsinki. I changed the actual content and eliminated some code to avoid trouble.
I will appreciate any help with the code and with the way I presented my problem, I also have to learn how to do that! Thanks!
FetchDataComponent is not really a react component since it does not render anything. It looks more like this is custom hook. To make that a hook, just make it simple function that calls other hooks:
const useFetchedData = () => {
const [data, setData] = useState({ product: [] })
useEffect(
() => {
(async () => {
const result = await axios.get(
'/products-data'
)
setData(result.data)
})()
}, [])
const mappedData = _.map(data.product, "proizvod_naziv")
return mappedData
}
export default useFetchedData;
And now you can use that hook's return value to get the data you want and pass that to other components.
const ReactSelectComponent = () => {
const mappedData = useFetchedData()
const options = mappedData.map(value => ({ value, label: value }))
return (<Select options={options}/>)
}
export default ReactSelectComponent;
Read more about custom hooks here.
You need to pass props to react-select see this answer for example:
<Select
closeMenuOnSelect={true}
options={colourOptions}
components={{
Input: (inputProps: InputProps) => (
<components.Input {...inputProps} />
),
}}
/>
How to pass props to custom components in react-select
Also you must return JSX from react components.