我想知道如何获取被点击的按钮的 id(因为它是未知的)。所以当按钮被点击时,我知道那个特定按钮的 id 是什么。页面上有很多按钮,我想知道按下了哪个按钮(它们都有唯一的 ID)。目前按钮看起来像这样:
<button key={uuidv4()} id={this.props.keyword} value={this.props.keyword} onClick={this.props.onClick} className="removeButton">Remove</button>
Run Code Online (Sandbox Code Playgroud) 运行代码时出现此错误。
超过最大更新深度。当组件重复调用componentWillUpdate或componentDidUpdate内部的setState时,可能会发生这种情况。React限制了嵌套更新的数量,以防止无限循环。
这是代码。它是引用。
69 | }
70 |
71 | addQuestion = () => {
> 72 | this.setState({numQuestions: this.state.numQuestions + 1});
73 | }
74 |
75 | render() {
131 | <div className="field">
132 | <div className="endButtons">
133 | <button id="addQuestionButton"
> 134 | onClick={this.addQuestion()}>Add Question</button>
135 | <input
136 | type="submit"
137 | value="Submit"
5 | import App from './App';
6 | import registerServiceWorker from './registerServiceWorker';
7 |
> 8 | ReactDOM.render(<App />, document.getElementById('root'));
9 | registerServiceWorker(); …Run Code Online (Sandbox Code Playgroud) 我正在制作一个允许用户构建测验的表单(这是我目前为止的代码)
var uuid = require("uuid-v4");
// Generate a new UUID
var myUUID = uuid();
// Validate a UUID as proper V4 format
uuid.isUUID(myUUID); // true
var questionNum = 0;
class App extends Component {
constructor(props) {
super(props);
this.state = {
key: uuid(),
title: "",
author: "",
questions: [],
answers: []
};
this.handleChange = this.handleChange.bind(this);
this.addQuestion = this.addQuestion.bind(this);
}
componentDidMount() {
// componentDidMount() is a React lifecycle method
this.addQuestion();
}
handleChange(event) {
const target = event.target;
const value = target.type === …Run Code Online (Sandbox Code Playgroud) var uuid = require('uuid-v4');
// Generate a new UUID
var myUUID = uuid();
// Validate a UUID as proper V4 format
uuid.isUUID(myUUID); // true
var questionNum = 0;
class App extends Component {
constructor(props){
super(props);
this.state = {
key: uuid(),
title: "",
author: "",
questions: [],
answers: []
}
this.handleChange = this.handleChange.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.type === "checkbox" ? target.checked : target.value;
const name = target.name;
this.setState({
[name]: value
});
}
addQuestion = …Run Code Online (Sandbox Code Playgroud)