我怎么能调用数组从一个组件显示在另一个组件使用类组件在React-js?



我有一个问题,我相信这是简单的,但我不能解决它,我没有找到类似的东西在互联网上。

我有一个React组件,它有一个像数组一样的状态,我试图把这个数组和另一个数组放入一个选择选项。

这是包含数组的组件:

import React,{Component} from "react";

class CarList extends Component{
constructor(props){
super(props);
this.state = {
carList: ['Jeep', 'Kwid','HB20','Ônix', 'Prisma', 'Gol quadrado']
}
}

render(){
return(
<div>
<select>
{this.state.carList.map( (item,ii)=>{
return(
<option key={ii}>{item}</option>
)
} )}
</select>
</div>
);
}
}

export default CarList;

这是在React-dom上渲染的组件:


import React from "react";
import { Component } from "react";
import CarList from "./components/Tabela";

class App extends Component{
constructor(props){
super(props);
this.state={

}
}

render(){
return(
<div>
<p>I got it! Here is the car list:</p>
<select>
{this.state.carList.map(  (item,x)=>{
return(
<option key={x}>{item}</option>
)
})}
</select>

</div>
)
}
}
export default App;```
Hey!
I tried to call with a map();
I tried to import the component;
I tried to call the array just before import the component;

不确定我是否理解正确。这里有一个选项:

检查你的App组件代码,select组件会给出与下面相同的结果:

render() {
return (
<div>
<p>I got it! Here is the car list:</p>
<CarList />
</div>
);
}