我有一个 <div>
标签,该标签包含了不同数量的按钮。单击每个按钮时,我有一个显示的反应变量。我的问题是,每当我单击任何按钮时,每个隐藏的文本都会显示;但是,我只希望这些按钮在我单击时显示它们的隐藏文本,以便它们的行为独特。
我尝试了一些有条件渲染的想法,但我不确定如何正确攻击。
我有一个为每个教堂创建按钮的功能。这是该功能:
createButtonsForChurches(arr) {
var listOfButtons = [];
for (var i = 1; i < arr.length; i++) { // loop through each church
var currentButton = (
<div>
<button onClick={this.toggle} className="churchButton">{arr[i].name}</button>
{
this.state.on && (
<div className="allContent">
<div className="individualContent">
{(arr[i].femaleStudents[0]) && this.createContentParagraphTextForArray(arr[i].femaleStudents, "Female Students")}
</div>
<div className="individualContent">
{(arr[i].femaleLeaders[0]) && this.createContentParagraphTextForArray(arr[i].femaleLeaders, "Female Leaders")}
</div>
<div className="individualContent">
{(arr[i].maleStudents[0]) && this.createContentParagraphTextForArray(arr[i].maleStudents, "Male Students")}
</div>
<div className="individualContent">
{(arr[i].maleLeaders[0]) && this.createContentParagraphTextForArray(arr[i].maleLeaders, "Male Leaders")}
</div>
</div>
)
}
</div>
);
listOfButtons.push(currentButton);
}
return listOfButtons;
}
您也可以看到为OnClick属性所调用的切换函数。这是我的切换方法和我的构造函数:
constructor(props) {
super(props);
this.toggle = this.toggle.bind(this);
this.state = {
on: false
}
}
toggle() {
this.setState({
on: !this.state.on
});
}
我不想放太多代码和复杂的东西 - ARR是一系列对象。这些对象包含4个不同的数组,并且每个数组中的每个数组都包含不同数量的对象。因此 - 阿尔是教堂的清单,其中包含教堂作为物体,每个教堂都包含4个阵列(女学生和男学生和领导者(,在这四个阵列中,每个阵列中的每个阵列都有成员作为对象
我不知道如何仅显示我单击的按钮的隐藏文本。会喜欢一些帮助。
您必须为按钮创建一个数组,并给出属性isHidden
以在切换上显示/隐藏数据。这是您问题的工作解决方案。
class App extends React.Component {
state = {
buttons: []
};
arr = [
{
name: "churchName1",
femaleStudents: ["student1", "student1"],
femaleLeaders: ["leaders1", "leaders1"],
maleStudents: ["student1", "student1"],
maleLeaders: ["leaders1", "leaders1"]
},
{
name: "churchName2",
femaleStudents: ["student2", "student2"],
femaleLeaders: ["leaders2", "leaders2"],
maleStudents: ["student2", "student2"],
maleLeaders: ["leaders2", "leaders2"]
},
{
name: "churchName3",
femaleStudents: ["student3", "student3"],
femaleLeaders: ["leaders3", "leaders3"],
maleStudents: ["student3", "student3"],
maleLeaders: ["leaders3", "leaders3"]
}
];
componentDidMount() {
// create buttons array
let buttons = [];
for (let item of this.arr) {
let buttonObj = { id: item.name, isHidden: true };
buttons.push(buttonObj);
}
this.setState({ buttons });
}
createContentParagraphTextForArray = (para1, para2) => {
return (
<div>
{para1} {para2}
</div>
);
};
createButtonsForChurches = arr =>
arr.map((item, index) => {
let isHidden =
this.state.buttons.length > 0
? this.state.buttons[index].isHidden
: true;
return (
<div key={item.name}>
<button
onClick={() => this.clickHandler(item.name)}
className="churchButton"
>
{item.name}
</button>
{!isHidden && (
<div className="allContent">
<div className="individualContent">
{item.femaleStudents[0] &&
this.createContentParagraphTextForArray(
item.femaleStudents,
"Female Students"
)}
</div>
<div className="individualContent">
{item.femaleLeaders[0] &&
this.createContentParagraphTextForArray(
item.femaleLeaders,
"Female Leaders"
)}
</div>
<div className="individualContent">
{item.maleStudents[0] &&
this.createContentParagraphTextForArray(
item.maleStudents,
"Male Students"
)}
</div>
<div className="individualContent">
{item.maleLeaders[0] &&
this.createContentParagraphTextForArray(
item.maleLeaders,
"Male Leaders"
)}
</div>
</div>
)}
</div>
);
});
clickHandler = buttonId => {
let buttons = this.state.buttons;
buttons.forEach(button => {
if (button.id === buttonId) {
button.isHidden = !button.isHidden;
}
});
this.setState({ buttons });
};
render() {
return (
<React.Fragment>{this.createButtonsForChurches(this.arr)}</React.Fragment>
);
}
}
ReactDOM.render(<App/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id='root'/>