[英]React Modals: Modal component only opens the first-clicked modal and none else
I am new to StackOverflow so pardon me for bad wording of the problem.我是 StackOverflow 的新手,所以请原谅我对问题的措辞不当。 I am learning React currently and as a small project, I am creating a course-search app which will filter the courses on the basis of input entered by the user from a JSON file and display them.我目前正在学习 React,作为一个小项目,我正在创建一个课程搜索应用程序,它将根据用户从 JSON 文件输入的内容过滤课程并显示它们。 I have added modals to each card component which should open on the click of a button within the card.我已将模态添加到每个卡片组件中,单击卡片中的按钮即可打开。 The problem I'm facing is that when I click the button, it only opens the modal first clicked one, and none else.我面临的问题是,当我单击按钮时,它只会打开第一个单击的模式,而不会打开其他模式。
Here is the code:这是代码:
import MyModal from '../Modal/Modal'
import courseList from "./courses.json";
class App extends Component {
state = {
search: "",
modal: false,
};
selectModal = (id) => {
this.setState({
modal: {
[id]: !this.state.modal
}
})
}
rendercourse = course => {
var dep = course.dep;
return (
<div className="col-md-3" style={{ marginTop: "20px" }}>
<Card className="card">
<CardBody className={dep}>
<CardTitle title={course.id}>
{course.code}
</CardTitle>
<CardText className="title">{course.name}</CardText>
<p className="ic">{course.ic}</p>
Units: {course.unit}<br />
<button onClick= {
this.selectModal.bind(this, course.code)}>More Info</button>
</CardBody>
<MyModal
displayModal={this.state.modal[course.code]}
coursename={course.name}
coursecode={course.code}
courseic={course.ic}
coursedep={course.dep}
courseunit={course.unit}
closeModal={this.selectModal} />
</Card>
</div>
);
};
onchange = e => {
this.setState({ search: e.target.value });
};
render() {
const { search } = this.state;
const filteredcourses = courseList.filter(course => {
return course.name.toLowerCase().indexOf(search.toLowerCase()) !== -1;
});
return (
<div className="flyout">
<main style={{ marginTop: "4rem" }}>
<div className="container">
<div className="row">
<div className="col-12">
<center>
<h3>
Search for a course
</h3>
</center>
</div>
<div className="col">
<Input
label="Enter the name of the course"
icon="search" className="in"
onChange={this.onchange}
/>
</div>
<div className="col" />
</div>
<div className="row">
{filteredcourses.map(course => {
return this.rendercourse(course);
})}
</div>
</div>
</main>
</div>
);
}
}
export default App;
And here is the modal component:这是模态组件:
const MyModal = props => {
function displayInfo () {
return (
<div>
<div>{props.coursename}</div>
<div>{props.courseic}</div>
<div>{props.courseunit}</div>
<div>{props.coursedep}</div>
<div>{props.coursecode}</div>
</div>
);
}
function closeModal (e) {
e.stopPropagation()
props.closeModal()
}
let modal = (
<div
className="modal"
onClick={ closeModal }>
<div className="modal-content"
onClick={ e => e.stopPropagation() }>
<span
className="close"
onClick={ closeModal }>×
</span>
<div className="modal-flex">
{displayInfo()}
</div>
</div>
</div>
)
return ( props.displayModal ? modal : null);
}
export default MyModal;
I want the card-specific modal to open up whenever the button is clicked.我希望在单击按钮时打开特定于卡片的模式。
Your selectModal function doesn't flip the state of each modal, but only that of the first one.您的 selectModal function 不会翻转每个模态的 state ,而只会翻转第一个模态的模态。 If any modal is defined in the state (be it the first or any other modal), this.state.modal will evaluate to true.如果在 state 中定义了任何模态(无论是第一个还是任何其他模态),this.state.modal 将评估为真。 (an object with at least some content is true) (至少有一些内容的object是真的)
To allow all your modals to be opened at the same time simply flip the value of the modal element in your modal state object.要允许同时打开所有模式,只需翻转模式中模式元素的值 state object。
this.state.modal[id]
is undefined by default, which will evaluate to boolean false. this.state.modal[id]
默认情况下未定义,其计算结果为 boolean false。
selectModal = (id) => {
this.setState({
modal: {
[id]: !this.state.modal[id]
}
})
}
If you'd rather only open one modal at a time, you should simply store the id of your modal in the state instead of tracking all modals in there:如果您宁愿一次只打开一个模态,您应该简单地将模态的 ID 存储在 state 中,而不是在其中跟踪所有模态:
selectModal = (id) => {
this.setState({
modal: id
})
}
To close a modal change your closeModal prop to pass undefined/false instead of the modal ID and change your modal display prop so it will perform the check whether your modal state and the coursecode match:要关闭模态,请更改您的 closeModal 道具以传递 undefined/false 而不是模态 ID 并更改您的模态显示道具,以便它执行检查您的模态 state 和课程代码是否匹配:
this.state.modal === course.code
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.