在这里,如何在名为“Selected Planets”的选项卡中仅显示“ListSelected”组件,而在名为“Planets List”选项卡的选项卡中显示其余整个组件(FetchPlanets)。如何在不同的选项卡中仅呈现“ListSelected”组件,而在不同的选项卡中呈现其余的整个组件(“FetchPlanets”)?所以它不应该刷新作为道具传递的选定行星
FetchPlanets.jsx
import React from "react";
import { ListGroup, Container } from "react-bootstrap";
import "./components.css";
import ListSelected from "./ListSelected";
export default class FetchPlanets extends React.Component {
constructor(props) {
super(props);
this.state = {
loading: true,
planets: null,
selectedPlanets: []
};
}
async componentDidMount() {
const url = "asdf";
const res = await fetch(url);
const data = await res.json();
this.setState({ planets: data, loading: false });
console.log(this.state.planets);
}
fetch = e => {
const data = this.state.selectedPlanets;
data.push(e.target.innerText);
this.setState({
selectedPlanets: data
});
};
render() {
const splanets = this.state.selectedPlanets.map(function(item) {
return <li> {item} </li>;
});
return (
<Container fluid>
{this.state.loading || !this.state.planets ? (
<div>Loading...</div>
) : (
<ListGroup>
{this.state.planets.map((planetnames, index) => {
return (
<ListGroup.Item
onClick={e => {
this.fetch(e);
}}
className="selectlg"
>
{planetnames.id}
</ListGroup.Item>
);
})}
</ListGroup>
)}
<ListSelected selp={splanets} />
</Container>
);
}
}