【发布时间】:2018-03-13 17:55:46
【问题描述】:
我试图在一个父组件下多次重用同一个组件。所有的 SortLabels 都共享相同的状态。因此,当单击一个元素并反映上一次单击的状态时。如何使它们成为具有不同状态的独立组件?
class SortLabel extends Component {
constructor(props, context) {
super(props, context);
this.state = {
order: 'asc',
};
this.toggle = this.toggle.bind(this);
}
toggle() {
this.setState({order: this.state.order === 'asc' ? 'desc' : 'asc'});
}
render() {
const {itemProperty, sortingProperty} = this.props;
return (
<TableSortLabel
key={`Sort ${itemProperty.get('id')}`}
id={`Sort ${itemProperty.get('id')}`}
active = {sortingProperty === itemProperty.get('id')}
onClick = {this.toggle}
direction={this.state.order}>
{itemProperty.get('description')}
</TableSortLabel>
)
}
}
更新 对此进行测试后,组件具有单独的状态。问题在于我处理状态的方式
【问题讨论】:
标签: reactjs react-native