2 回答
TA贡献1895条经验 获得超3个赞
输出:
https://i.stack.imgur.com/xloeO.gif
完整示例:
import React, { Component } from "react";
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
a: ["Apple", "pear", "plum", "currant", "strawberry"],
b: ["Amelia", "Oliver", "Sophie", "Alfie", "Jacob"],
c: ["mercedes", "bmw", "audi", "volkswagen", "hyundai"],
index: null
};
}
setIndex = i => {
console.log(i);
this.setState({
index: i
});
console.log(this.state.index);
};
render() {
return (
<div>
{this.state.index !== null && (
<div>
<List
a={this.state.a[this.state.index]}
b={this.state.b[this.state.index]}
/>
</div>
)}
<div>
<ul>
{this.state.a.map((arr, idx) => (
<li
onClick={() => {
console.log("hi");
this.setIndex(idx);
}}
>
{arr}
</li>
))}
</ul>
</div>
</div>
);
}
}
class List extends Component {
constructor(props) {
super(props);
}
render() {
return (
<div>
<ul>
<li>{this.props.a}</li>
<li>{this.props.b}</li>
</ul>
</div>
);
}
}
您可以在此处查看工作示例:stackblitz
TA贡献1811条经验 获得超4个赞
您可以在函数内添加onClick一个,如下所示limap
itemsThatMatchIndex = []
getItemsInBAndCThatMatch(index) {
this.itemsThatMatchIndex = [this.b[index], this.c[index]];
}
render() {
let pp = this.a.map((arr, idx) => {
return (<li key={idx}>
<button onClick={() => this.getItemsInBAndCThatMatch(idx)}>
{this.a[idx]}
</button>
</li>);
});
return (
<div>
<div>
<ul>{pp}</ul>
</div>
<List data={this.itemsThatMatchIndex}/>
</div>
);
}
添加回答
举报