【发布时间】:2018-05-31 16:04:59
【问题描述】:
我有一个可以保存位置名称的位置应用程序。 我正在尝试通过单击为每个保存的位置设置一个红色边框。 它所做的是改变所有类别的边框颜色。 我该如何应用它?
class Categories extends Component {
constructor(props) {
super(props);
this.state = {
term: '',
categories: [],
selectedCategories: [],
hidden: true,
checkboxState: true
};
}
toggle(e) {
this.setState({
checkboxState: !this.state.checkboxState
})
}
onChange = (event) => {
this.setState({ term: event.target.value });
}
addCategory = (event) => {
if (this.state.term === '') {
alert('Please name your category!')
} else {
event.preventDefault();
this.setState({
term: '',
categories: [...this.state.categories, this.state.term]
});
}
}
render() {
return (
<div className="categories">
<h1>Categories</h1>
<div className='actions'>
<button className="delete" onClick={this.deleteCategory}>Delete</button>
<button className="edit" onClick={this.editCategory}>Edit</button>
</div>
<p>To add new category, please enter category name</p>
<form className="App" onSubmit={this.addCategory}>
<input value={this.state.term} onChange={this.onChange} />
<button>Add</button>
</form>
{this.state.categories.map((category, index) =>
<button
key={index}
style={this.state.checkboxState ? { borderColor: '' } : { borderColor: 'red' }}
checked={this.state.isChecked}
onClick={this.toggle.bind(this)}>
{category}</button>
)}
</div >
);
}
}
我希望能够单独控制每个选定的类别,以便能够删除和编辑主题。
【问题讨论】:
-
你能澄清一下吗?因此,如果选择了一个类别,则不应选择其他类别,对吗?这意味着他们应该有某种沟通方式。或者一些监管他们的父实体
-
您可以通过单击选择一个类别,然后它变成了红色边框。问题是创建多个类别后,单击一个会使所有类别变为红色。
-
这仍然没有回答一个问题。您希望一次只选择一个类别还是多个类别?如果是一个,则父容器必须管理它。
-
一次只有一个。
-
知道了,所以你需要一个父容器。父容器保存所有类别并将方法传递给它们的道具。当您单击类别时,您会将信息从类别传递给 parent 方法。我会写一个例子并将其发布到答案中
标签: javascript reactjs list