reactjs - RadioGroup Onchange 没有在 React 中触发
问题描述
我无法找到为什么 onChange 事件在更改检查后没有触发的根本原因,但是,当组件首次加载时 onChange 被调用。
我准备了一个带有一些示例数据的 CodeSandbox。
这是组件,
import React from "react";
import styled from "styled-components";
import { checklists } from "./checklists";
import { questions } from "./questions";
const RadioOption = styled.input`
margin: 10px;
width: 16px;
height: 16px;
`;
const RadioGroup = (props) => {
return React.Children.map(props.children, (child) => {
if (child.type === RadioOption)
return React.cloneElement(child, {
type: "radio",
defaultChecked: props.value === child.props.value,
name: props.name,
disabled: props.disabled,
onChange: props.handleChange
});
return child;
});
};
class RadioGroupDemo extends React.Component {
state = {
checklist: "test_checklist_checklist",
questions: questions,
checklists: checklists
};
constructor(props) {
super(props);
this.onRadioChange = this.onRadioChange(this);
this.keyCount = 0;
this.getKey = this.getKey.bind(this);
}
onRadioChange = (e) => {
debugger;
console.log("handleRadios", e);
//let q = this.state.questions;
};
getKey() {
return this.keyCount++;
}
//first render
render() {
console.log(this.state.checklists);
return (
<div style={{ maxHeight: "600px", overflow: "auto" }}>
{this.state.checklist &&
this.state.checklists.questions[this.state.checklist].map(
(question) => {
//debugger;
let question_id = question.question_id;
return (
<div key={`${this.state.checklist}_${question.id}`}>
{question.title}
<div style={{ width: "120px", flex: "0 0 120px" }}>
{(() => {
if (
typeof this.state.questions[question.id] !== "undefined"
) {
return (
<RadioGroup
name={`field_question_${question.id}`}
disabled={this.state.readOnly}
value={
typeof this.state.questions[question.id].answer ==
"undefined"
? this.state.questions[question.id].answer
: Object.keys(this.state.questions)
.reduce(
(arr, key) =>
arr.concat(this.state.questions[key]),
[]
)
.find((q) => {
return q.question_id === question_id;
}).answer
}
onChange={this.onRadioChange}
>
<RadioOption key={this.getKey()} value="yes" />
<RadioOption key={this.getKey()} value="no" />
<RadioOption key={this.getKey()} value="na" />
</RadioGroup>
);
}
})()}
</div>
</div>
);
}
)}
</div>
);
}
}
export default RadioGroupDemo;
请查看代码框并告诉我事件未触发的原因。谢谢
解决方案
问题
您将一个onChange
道具传递给RadioGroup
但访问一个handleChange
不存在的道具。
const RadioGroup = (props) => {
return React.Children.map(props.children, (child) => {
if (child.type === RadioOption)
return React.cloneElement(child, {
type: "radio",
defaultChecked: props.value === child.props.value,
name: props.name,
disabled: props.disabled,
onChange: props.handleChange, // <-- accessed as handleChange
});
return child;
});
};
在组件中
<RadioGroup
name={`field_question_${question.id}`}
disabled={this.state.readOnly}
value={
typeof this.state.questions[question.id].answer ==
"undefined"
? this.state.questions[question.id].answer
: Object.keys(this.state.questions)
.reduce(
(arr, key) =>
arr.concat(this.state.questions[key]),
[]
)
.find((q) => {
return q.question_id === question_id;
}).answer
}
onChange={this.onRadioChange} // <-- passed as onChange
>
在RadioGroupDemo
你也不要绑定this
到onRadioChange
处理程序。这个绑定不是必需的,因为onRadioChange
它被声明为一个箭头函数,this
是自动绑定的。
constructor(props) {
super(props);
this.onRadioChange = this.onRadioChange(this); // <-- here
this.keyCount = 0;
this.getKey = this.getKey.bind(this);
}
解决方案
this
删除在构造函数中绑定到处理程序的尝试。
constructor(props) {
super(props);
this.keyCount = 0;
this.getKey = this.getKey.bind(this);
}
访问正确的道具。
const RadioGroup = (props) => {
return React.Children.map(props.children, (child) => {
if (child.type === RadioOption)
return React.cloneElement(child, {
type: "radio",
defaultChecked: props.value === child.props.value,
name: props.name,
disabled: props.disabled,
onChange: props.onChange // <-- onChange
});
return child;
});
};
推荐阅读
- c# - 在 Windows 剪贴板中存储图像宽度 alpha 通道
- javascript - 使用 JS 更改 PHP 包含在 HTML 页面中
- python - KeyError:'password1':password1 = self.cleaned_data['password1']
- html - CSS 网格布局:用更少的输入指定网格位置?
- python - 每次机器人运行时,如何更新函数的特定参数?
- git - Git 中提交和合并的概念(适用于了解 SVN 的人)
- python - 如何修复没有显示名称和 $DISPLAY 环境变量 python 错误
- slack - 单击选择菜单会在松弛模式视图中打开另一个菜单
- netbeans - 有没有办法使用快捷方式或 Netbeans 中的快捷方式插件在拆分的窗口之间移动?
- macos - Applescript 让 iTunes 通过 Apple Music URL 播放专辑?