This app shows a number that you can increase or decrease by pressing buttons. It uses Redux Toolkit to keep the number in a central store.
import React from 'react';
import { Provider, useSelector, useDispatch } from 'react-redux';
import { configureStore, createSlice } from '@reduxjs/toolkit';
import { Text, View, Button } from 'react-native';
const counterSlice = createSlice({
name: 'counter',
initialState: { value: 0 },
reducers: {
increment: state => { state.value += 1; },
decrement: state => { state.value -= 1; }
}
});
const store = configureStore({
reducer: {
counter: counterSlice.reducer
}
});
function Counter() {
const count = useSelector(state => state.counter.value);
const dispatch = useDispatch();
return (
<View style={{ padding: 20, alignItems: 'center' }}>
<Text style={{ fontSize: 24, marginBottom: 10 }}>Count: {count}</Text>
<Button title="Increase" onPress={() => dispatch(counterSlice.actions.increment())} />
<View style={{ height: 10 }} />
<Button title="Decrease" onPress={() => dispatch(counterSlice.actions.decrement())} />
</View>
);
}
export default function App() {
return (
<Provider store={store}>
<Counter />
</Provider>
);
}