如何获取 Reactstrap 的 CustomInput Switch 组件的状态,以及如何从数组映射开关?
How to get state of Reactstrap's CustomInput Switch component, and how to map switches from an array?
<FormGroup>
<div>
{this.props.diseases.map((disease, index) => (
<FormGroup>
<CustomInput
type="switch"
id="exampleCustomSwitch"
key={disease}
disease={disease}
onClick={(disease) => this.props.toggle(disease)}
label={disease}
/>
</FormGroup>
))
}
</div>
</FormGroup>
我想知道开关的状态,是打开还是关闭。不确定我该怎么做?我是否要传递某种默认值,其中 0 表示关闭,1 表示打开?
目前,开关从数组中正确映射,但打开或关闭仅适用于第一个开关。因此,如果我单击任何其他开关,由于某种原因第一个开关会切换。
对于第 1 点,您可以使用 e.target.checked
检查特定 CustomInput
的 true/false 状态;检查 this stackblitz 以查看它是否有效
对于第 2 点,如果您共享现有代码,将更容易为您的特定场景提供帮助
相关js:
class App extends Component {
constructor() {
super();
this.state = {
name: "World to React",
log: []
};
this.customInputSwitched.bind(this);
}
customInputSwitched(buttonName, e) {
let newStr = `we received ${e.target.checked} for ${buttonName}...`;
console.log(newStr);
let newLog = [...this.state.log, newStr];
this.setState({ log: newLog });
}
render() {
var testName = "modal for testing - click here";
return (
<div>
<Hello name={this.state.name} />
<p>Start editing to see some magic happen :)</p>
<Form>
<FormGroup>
<Label for="exampleCheckbox">Switches</Label>
<div>
<CustomInput
type="switch"
id="exampleCustomSwitch"
name="customSwitch"
label="Turn on this custom switch"
onChange={this.customInputSwitched.bind(this, "button1")}
/>
<CustomInput
type="switch"
id="exampleCustomSwitch2"
name="customSwitch"
label="Or this one"
onChange={this.customInputSwitched.bind(this, "button2")}
/>
<CustomInput
type="switch"
id="exampleCustomSwitch3"
label="But not this disabled one"
disabled
/>
<CustomInput
type="switch"
id="exampleCustomSwitch4"
label="Can't click this label to turn on!"
htmlFor="exampleCustomSwitch4_X"
disabled
/>
</div>
</FormGroup>
</Form>
{this.state.log}
</div>
);
}
}
更新#1:根据下面提问者的评论
您在 https://stackblitz.com/edit/react-rcqlwq
的代码中存在一些问题
- 你必须在构造函数中实例化登录状态
customInputSwitched
函数应该传递特定按钮的参数,而不是硬编码的 'button1' - 所以我们添加疾病的索引号
- 所有按钮的ID不能相同'exampleCustomSwitch',所以我们在ID上加上索引号就可以了
- 映射为数组的最佳做法是同时包含一个索引,这有好处(如下两点所示)
你的 code/stackblitz 的相关工作 JS:
class App extends Component {
constructor() {
super();
this.state = {
diseases: [
"Normal",
"Over inflated lungs",
"Pneumonia",
"Pneumothorax",
"Congestive cardiac failure",
"Consolidation",
"Hilar enlargement",
"Medical device",
"Effusion"
],
log: []
};
this.customInputSwitched.bind(this);
}
customInputSwitched(buttonName, e) {
let newStr = `we received ${e.target.checked} for ${buttonName}...`;
console.log(newStr);
let newLog = [...this.state.log, newStr];
this.setState({ log: newLog });
}
render() {
return (
<div>
<p>Start editing to see some magic happen :)</p>
<Form>
<FormGroup>
<Label for="exampleCheckbox">Switches</Label>
{this.state.diseases.map((disease, index) => {
//console.log(disease, index);
let idName = "exampleCustomSwitch"+index;
return (
<div key={index}>
<CustomInput
type="switch"
id={idName}
name="customSwitch"
label={disease}
onChange={this.customInputSwitched.bind(this, "button"+index)}
/>
</div>
);
}
)}
</FormGroup>
</Form>
{this.state.log}
</div>
);
}
}
<FormGroup>
<div>
{this.props.diseases.map((disease, index) => (
<FormGroup>
<CustomInput
type="switch"
id="exampleCustomSwitch"
key={disease}
disease={disease}
onClick={(disease) => this.props.toggle(disease)}
label={disease}
/>
</FormGroup>
))
}
</div>
</FormGroup>
我想知道开关的状态,是打开还是关闭。不确定我该怎么做?我是否要传递某种默认值,其中 0 表示关闭,1 表示打开?
目前,开关从数组中正确映射,但打开或关闭仅适用于第一个开关。因此,如果我单击任何其他开关,由于某种原因第一个开关会切换。
对于第 1 点,您可以使用 e.target.checked
检查特定 CustomInput
的 true/false 状态;检查 this stackblitz 以查看它是否有效
对于第 2 点,如果您共享现有代码,将更容易为您的特定场景提供帮助
相关js:
class App extends Component {
constructor() {
super();
this.state = {
name: "World to React",
log: []
};
this.customInputSwitched.bind(this);
}
customInputSwitched(buttonName, e) {
let newStr = `we received ${e.target.checked} for ${buttonName}...`;
console.log(newStr);
let newLog = [...this.state.log, newStr];
this.setState({ log: newLog });
}
render() {
var testName = "modal for testing - click here";
return (
<div>
<Hello name={this.state.name} />
<p>Start editing to see some magic happen :)</p>
<Form>
<FormGroup>
<Label for="exampleCheckbox">Switches</Label>
<div>
<CustomInput
type="switch"
id="exampleCustomSwitch"
name="customSwitch"
label="Turn on this custom switch"
onChange={this.customInputSwitched.bind(this, "button1")}
/>
<CustomInput
type="switch"
id="exampleCustomSwitch2"
name="customSwitch"
label="Or this one"
onChange={this.customInputSwitched.bind(this, "button2")}
/>
<CustomInput
type="switch"
id="exampleCustomSwitch3"
label="But not this disabled one"
disabled
/>
<CustomInput
type="switch"
id="exampleCustomSwitch4"
label="Can't click this label to turn on!"
htmlFor="exampleCustomSwitch4_X"
disabled
/>
</div>
</FormGroup>
</Form>
{this.state.log}
</div>
);
}
}
更新#1:根据下面提问者的评论
您在 https://stackblitz.com/edit/react-rcqlwq
的代码中存在一些问题- 你必须在构造函数中实例化登录状态
customInputSwitched
函数应该传递特定按钮的参数,而不是硬编码的 'button1' - 所以我们添加疾病的索引号- 所有按钮的ID不能相同'exampleCustomSwitch',所以我们在ID上加上索引号就可以了
- 映射为数组的最佳做法是同时包含一个索引,这有好处(如下两点所示)
你的 code/stackblitz 的相关工作 JS:
class App extends Component {
constructor() {
super();
this.state = {
diseases: [
"Normal",
"Over inflated lungs",
"Pneumonia",
"Pneumothorax",
"Congestive cardiac failure",
"Consolidation",
"Hilar enlargement",
"Medical device",
"Effusion"
],
log: []
};
this.customInputSwitched.bind(this);
}
customInputSwitched(buttonName, e) {
let newStr = `we received ${e.target.checked} for ${buttonName}...`;
console.log(newStr);
let newLog = [...this.state.log, newStr];
this.setState({ log: newLog });
}
render() {
return (
<div>
<p>Start editing to see some magic happen :)</p>
<Form>
<FormGroup>
<Label for="exampleCheckbox">Switches</Label>
{this.state.diseases.map((disease, index) => {
//console.log(disease, index);
let idName = "exampleCustomSwitch"+index;
return (
<div key={index}>
<CustomInput
type="switch"
id={idName}
name="customSwitch"
label={disease}
onChange={this.customInputSwitched.bind(this, "button"+index)}
/>
</div>
);
}
)}
</FormGroup>
</Form>
{this.state.log}
</div>
);
}
}