简体   繁体   English

如何在 React 中将数组作为结果从一个功能组件传递到另一个功能组件

[英]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.jsReactSelectComponent.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;

Read more about custom hooks here.在此处阅读有关自定义挂钩的更多信息。

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.

相关问题 如何从一个反应功能组件调用功能方法到另一个反应功能组件? - How to call a functional method from one react functional component to another react functional component? React:为什么我不能将数据从一个功能组件传递到另一个功能组件? - React: Why can't I pass data from one functional component to another functional component? 如何将函数作为道具从一个功能组件传递到另一个? - How to pass function as props from one functional component to another? 如何通过 React Router Link 将值从功能组件传递到另一个功能组件? - How can I pass values from a functional component through a React Router Link to another functional component? 如何使用功能组件将数据从一个组件传递到另一个组件,而无需重新渲染或 jsx,例如 function - How to pass data from one component to another in react without re-render or jsx like from a function using functional components 保存从 promise 返回的数据并将其传递给反应中的另一个组件 - Save and pass data returned from a promise to functional another component in react 如何在reactjs中将数据数组从一个组件传递到另一个组件 - How to pass array of data from one component to another component in reactjs 如何在 React js 中将多个变量从一个功能组件传递到另一个? - How to pass multiple variables from one functional component to other in React js? 如何在react js中将值从一个组件传递到另一个组件 - how to pass value from one component to another in react js 如何在 React.JS 中将数据从一个组件传递到另一个组件 - How to pass data from one component to another in React.JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM