简体   繁体   English

如何将对象作为道具传递给 React.js 中的子功能组件?

[英]How to pass an object as props to a child Functional Component in React.js?

I have this code with a config file testType :我有一个带有配置文件testType代码:

import React, { Component } from 'react';
import Select from "./components/Select/Select";

class App extends Component {
  state = {
    testType : [
      {value : "test1", name : "Test1"},
      {value : "test2", name : "Test2"},
    ]
  }
    render() {
        return (
            <>
              <Select {...this.state.testType}/>
              <Select {...this.state.testType}/>
            </>
            );
    }
}
export default App;

Which I use to pass as props to a Select Component, below is the code for it Select.js :我用来作为道具传递给 Select 组件,下面是Select.js的代码:

import React from "react";
const selectoption = (props) => (
<select className="custom-select">
    <option value={props.value}>{props.name}</option>
</select>
);

export default selectoption;

But it does not work, I don't see the props test1 and test2 in the select element.但它不起作用,我在选择元素中没有看到props test1 和 test2。

The problem in your code is when you are doing <Select {...this.state.testType}/> the below is the data that will be received as prop in the Select component.您的代码中的问题是当您执行<Select {...this.state.testType}/> ,以下是将作为Select组件中的prop接收的数据。 So basically the array is getting converted to below format in the Select component.所以基本上数组在Select组件中被转换为以下格式。

{
  "0": {
    "value": "test1",
    "name": "Test1"
  },
  "1": {
    "value": "test2",
    "name": "Test2"
  }
}

So, instead of spreading it that way if it's being passed as list={[...this.state.testType]} then the passed array can be accessed in Select component as props.list or in the component itself it can be spread like const Select = ({ list }) => {...} .因此,如果它作为list={[...this.state.testType]}传递, list={[...this.state.testType]}这种方式传播它,那么传递的数组可以在Select组件中作为props.list访问,也可以在组件本身中传播像const Select = ({ list }) => {...} The passed list can be looped through using Array.map and then render all the options.传递的list可以使用Array.map循环,然后呈现所有选项。

 const { Component, Fragment } = React; class App extends Component { state = { testType: [ { value: "test1", name: "Test1" }, { value: "test2", name: "Test2" } ] }; render() { return ( <Fragment> <Select list={[...this.state.testType]} /> <Select list={[...this.state.testType]} /> </Fragment> ); } } const Select = ({ list }) => ( <select className="custom-select"> {list.map(option => ( <option key={option.value} value={option.value}>{option.name}</option> ))} </select> ); ReactDOM.render(<App />, document.getElementById("react"));
 <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.8.0/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.8.0/umd/react-dom.production.min.js"></script> <div id="react"></div>

For more info about rendering list elements in React please refer here .有关在 React 中渲染列表元素的更多信息,请参阅此处

I hope you are doing well.我希望你做得很好。 I've just read your descriptions carefully.我刚刚仔细阅读了你的描述。 Data you have passed through props is an array of objects.你通过 props 传递的数据是一个对象数组。 So in the selectoption components the "prop" is an array of objects.所以在 selectoption 组件中,“prop”是一个对象数组。 You cannot find "value" and "name" of this props.您找不到此道具的“值”和“名称”。 You must map this array to get data of each options.您必须映射此数组以获取每个选项的数据。

Or you can change the code like this.或者您可以像这样更改代码。

<Select {...this.state.testType[0]}/> <选择{...this.state.testType[0]}/>

<Select {...this.state.testType[1]}/> <选择{...this.state.testType[1]}/>

Thank you for your vote.感谢您的投票。 I am a dedicated and reliable Full-Stack Developer with over 10 years of experience in Web Development.我是一名敬业且可靠的全栈开发人员,拥有超过 10 年的 Web 开发经验。 I have strong skills and experience with React,Vue,Angular Frontend Development and Backend Development with RESTful APIs using Node.js and Laravel.我在使用 Node.js 和 Laravel 使用 RESTful API 进行 React、Vue、Angular 前端开发和后端开发方面具有很强的技能和经验。 I can help you with my strong development skills.我可以用我强大的开发技能来帮助你。 You will be happy with my quality of code and fast delivery.您会对我的代码质量和快速交付感到满意。 I am full time available and I can overlap at least 8 hrs a day in your timezone.我有全职工作,我可以在您的时区每天至少重叠 8 小时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React.js - 如何将属性对象传递给子组件? - React.js — How to pass properties object to child component? 如何在 React.js 功能组件中将子组件 state 传递给父组件 state? - How to pass child component state to parent component state in React.js functional components? 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 如何使用 react.js 中的编程路由将道具从子组件(addform)传递到主应用程序组件 - How to pass props from child component (addform) to main app component using programmatic routing in react.js React.js如何从this.props数组中将index作为prop传递给子组件 - React.js how to pass in index as prop to child component from this.props array React.js - 子组件道具未定义 - React.js - child component props undefined 在 React.js 中将 props 传递给父组件 - Pass props to parent component in React.js 如何在 React.js 中将 state 作为道具从父母传递给孩子,然后从该孩子传递给它的孩子 - How to pass state from parent to child as props then from that child to it s child as props in React.js 从 React.js 中的父功能组件继承 props - Inherit props from parent functional component in React.js 如何使用React.js将状态属性传递给构造函数中的子组件? - How to pass state property to child component in constructor with React.js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM