Bu bölüm, başlık kısmına Button oluşturma ve Button üzerinden işlem yapmaya ayrılmıştır.
Button state bölümünde tutulan bir değişkeni arttıracak ve arttırma sonucu bir Text bileşeni içinde gösterilecektir. Ekran7.js adındaki dosya içeriği:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
import React, {Component} from 'react'; import {Text, View, Button} from 'react-native'; type Props = {}; export default class Ekran7 extends Component<Props> { static navigationOptions = ({ navigation }) => { return { title: 'Ekran 7', headerRight: ( <Button onPress={navigation.getParam('increaseCount')} title="+1" color="#000" /> ), }; }; componentDidMount() { this.props.navigation.setParams({ increaseCount: this._increaseCount }); } state = { count: 0, }; _increaseCount = () => { this.setState({ count: this.state.count + 1 }); }; render() { return ( <View> <Text>{this.state.count}</Text> </View> ); } } |
headerRight başlık kısmının sağ kısmını işaret eder. Bu kısma bir Button eklenmiş ve increaseCount değişkenine bağlanmıştır. Diğer taraftan componentDidMount “this._increaseCount” ile increaseCount değişkenini işaret eder. _increaseCount fonksiyonu State bölümündeki count değişkenini günceller. render kısmında sonuç bir Text içinde gösterilir.