[英]How to pass array as result from one functional component to another in React
I want to pass data stored in const mappedData
from FetchDataComponent.js
to ReactSelectComponent.js
.我想将存储在
const mappedData
中的数据从FetchDataComponent.js
到ReactSelectComponent.js
。 Is there any way of doing this with functional components?有没有办法用功能组件做到这一点? With code under i get
Error: Objects are not valid as a React child
使用下面的代码,我得到
Error: Objects are not valid as a React child
FetchDataComponent.js FetchDataComponent.js
import React,{useState, useEffect} from "react";
import axios from "axios";
import _ from "lodash";
const FetchDataComponent = () => {
const [data, setData] = useState({product: []})
useEffect(
() => {
(async () => {
const result = await axios.get(
'/products-data'
)
setData(result.data)
})()
}, [])
console.log("data.product:")
console.log(data.product)
const mappedData = _.map(data.product, "proizvod_naziv")
console.log("mappedData:")
console.log(mappedData)
console.log(typeof mappedData)
return ({mappedData});
}
export default FetchDataComponent;
ReactSelectComponent.js反应选择组件.js
import React, {useState} from "react";
import Select from "react-select";
const ReactSelectComponent = () => {
// place for data from FetchDataComponent.js
const options = [
{
// value: ,
// label:
},
{value: 'strawberry', label: 'Strawberry'},
{value: 'vanilla', label: 'Vanilla'}
]
return (<Select options={options}/>)
}
export default ReactSelectComponent;
console.log(mappedData):控制台.log(映射数据):
(3) […]
0: "M-Z-8Nm"
1: "M-P-10Nm"
2: "V-H"
length: 3
<prototype>: Array []
console.log(typeof mappedData):控制台.log(映射数据类型):
object
FetchDataComponent
is not really a react component since it does not render anything. FetchDataComponent
并不是真正的反应组件,因为它不渲染任何东西。 It looks more like this is custom hook.它看起来更像是自定义钩子。 To make that a hook, just make it simple function that calls other hooks:
要使它成为一个钩子,只需将其简化为调用其他钩子的 function:
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;
You need to pass props to react-select
see this answer for example:您需要将道具传递给
react-select
请参阅此答案,例如:
<Select
closeMenuOnSelect={true}
options={colourOptions}
components={{
Input: (inputProps: InputProps) => (
<components.Input {...inputProps} />
),
}}
/>
How to pass props to custom components in react-select 如何在 react-select 中将 props 传递给自定义组件
Also you must return JSX
from react components.您还必须从反应组件返回
JSX
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.