官术网_书友最值得收藏!

  • React Native Blueprints
  • Emilio Rodriguez Martinez
  • 147字
  • 2021-07-02 15:19:58

Adding state to our screen

Let's add some initial state to our ShoppingList screen to populate the list with actual dynamic data. We will start by creating a constructor and setting the initial state there:

/*** ShoppingList.js ***/

...
constructor(props) {
super(props);
this.state = {
products: [{ id: 1, name: 'bread' }, { id: 2, name: 'eggs' }]
};
}
...

Now, we can render that state inside of <List> (inside the render method):

/*** ShoppingList.js ***/

...
<List>
{
this.state.products.map(p => {
return (
<ListItem
key={p.id}
>
<Body>
<Text style={{ color: p.gotten ? '#bbb' : '#000' }}>
{p.name}
</Text>
</Body>
<Right>
<CheckBox
checked={p.gotten}
/>
</Right>
</ListItem>
);
}
)}
</List>
...

We now rely on a list of products inside our component's state, each product storing an id, a name, and gotten properties. When modifying this state, we will automatically be re-rendering the list.

Now, it's time to add some event handlers, so we can modify the state at the users' command or navigate to the AddProduct screen.

主站蜘蛛池模板: 肇源县| 澄江县| 凤阳县| 泰和县| 乌什县| 肇州县| 永定县| 丰原市| 当阳市| 吕梁市| 汝南县| 育儿| 陆丰市| 娱乐| 铜陵市| 南丹县| 西城区| 金华市| 开化县| 长岭县| 和龙市| 芒康县| 改则县| 贵阳市| 通渭县| 姚安县| 阳新县| 漳浦县| 依安县| 兴隆县| 凤城市| 清原| 宁晋县| 克拉玛依市| 手游| 安康市| 乐亭县| 玉溪市| 阿城市| 景洪市| 南充市|