【发布时间】:2019-02-06 07:15:52
【问题描述】:
我正在做一个实验项目,当我遇到这个我无法解释它的行为的奇怪事情时,我正在尝试使用 javascript。我正在使用带有 CDN 的 bootstrap 4。 SandboxCode
import React from 'react';
const Button = props => {
const showProps = e => {
e.preventDefault();
console.log(props.day, props.slot);
};
return (
<div>
<div data-toggle="modal" data-target="#myModal">
Click Me!
</div>
<div className="modal fade" id="myModal">
<div className="modal-dialog">
<div className="modal-content">
<div className="modal-header">
<h4 className="modal-title">Add / Edit Slot</h4>
<button type="button" className="close" data-dismiss="modal">
×
</button>
</div>
<div className="modal-body">
<form>
<div className="form-group">
<label>
<h6>Faculty</h6>
</label>
</div>
<div className="form-group">
<label>
<h6>Venue</h6>
</label>
</div>
<div className="form-group">
<label>
<h6>Subject</h6>
</label>
</div>
<button
type="submit"
className="btn btn-success btn-cons btn-block"
onClick={showProps}
>
Save
</button>
</form>
</div>
<div className="modal-footer">
<button
type="button"
className="btn btn-danger btn-block"
data-dismiss="modal"
>
Close
</button>
</div>
</div>
</div>
</div>
</div>
);
};
export default Button;
该组件提供了一个按钮,当在模态框内单击“保存”按钮时,它会打开一个模态框,然后它会控制台的两个道具(日期和插槽)。
它的父组件是主要组件,然后在要渲染的App.js文件中使用。
import React, { Component } from "react";
import Button from "./Button/Button";
export default class Main extends Component {
render() {
return ["Monday", "Tuesday"].map((day, dayIndex) => {
return [0, 1].map((slot, slotIndex) => {
return (
<Button key={dayIndex + "-" + slotIndex} day={day} slot={slotIndex} />
);
});
});
}
}
异常结果:- 在模态中单击按钮并单击保存按钮后,每个按钮应根据道具控制不同的数据。
输出:- 所有保存按钮点击输出相同的东西。 我已经坚持了两天了,我已经尝试了很多东西。
【问题讨论】:
-
嗨伙计,我已经用你的代码创建了一个沙箱,它似乎运行良好,检查你是否可以记录你的期望:codesandbox.io/s/mj8yryw14p
-
@MosèRaguzzini 我没有看到你使用引导程序,所以你可以试试codesandbox.io/s/…这个
-
在 react 中使用 id 确实是错误的,因为它是一种反模式。您应该使用 refs 代替或使用适当的 UI 工具包,如 React Semantic UI
-
这和函数式编程有什么关系?
标签: javascript reactjs functional-programming bootstrap-4 frontend