【发布时间】:2017-04-10 08:57:49
【问题描述】:
应用程序从外部服务器接收一些这样的列表。
[
{'type' : 'fruits', 'name' : 'banana'},
{'type' : 'fruits', 'name' : 'apple'},
{'type' : 'vegetables', 'name' : 'carrot'},
{'type' : 'vegetables', 'name' :'onion'}
]
然后我会通过mapStateToProps将它保存到redux store,以便在某些组件中使用它。
const mapStateToProps = (state) => {
return {
foodList: state.foodList
}
};
现在我可以通过 this.props.foodList 使用它
我想要做的是有一些单选按钮(所有,水果,蔬菜)。如果我单击该按钮,则应显示正确的列表。很简单(所有按钮 => 所有列表,水果按钮 => 水果列表)。
我该怎么做?
我尝试将 if 语句添加到 this.props.foodList.map(...),但出现了一些语法问题。我不需要再次从服务器检索数据。我怎么能简单地做到这一点?
class someComponent extends Component {
constructor(props){
super(props);
this.state = {
selectedIndex: 0
}
}
render() {
return(
<RadioGroup
selectedIndex={0}
onSelect = { (index)=> this.setState({selectedIndex : index}) }
>
<RadioButton value={0}>
<Text>All</Text>
</RadioButton>
<RadioButton value={1}>
<Text>Fruits</Text>
</RadioButton>
<RadioButton value={2}>
<Text>Vegetables</Text>
</RadioButton>
</RadioGroup>
<View>
<Text>Food List</Text>
<List>
{
this.props.foodList.map((l, i) => (
<ListItem
key= {i}
title= {l.name}
subtitle = {l.type}
/>
))
}
</List>
</View>
)
}
}
【问题讨论】:
标签: reactjs react-native react-redux