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