반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 센토스
- node
- 티스토리챌린지
- linux
- webpack
- qunit
- jest
- VirtualBox
- 맥
- 네트워크
- xcode
- build
- TensorFlow
- centos
- localserver
- ReactNative
- MachineLearning
- 리눅스
- react
- androidstudio
- PYTHON
- unittest
- Android
- IOS
- MAC
- Chrome
- vsCode
- 오블완
- 개발
Archives
- Today
- Total
로메오의 블로그
[React Native] redux 본문
반응형
모듈 설치
$ npm install redux
$ npm install react-redux
$ npm install redux-thunk
파일 생성
$ mkdir src
$ mkdir src/redux
$ mkdir src/screens
$ touch src/redux/actions.tsx
$ touch src/redux/reducer.tsx
$ touch src/redux/store.tsx
$ touch src/screen/HomeScreen.tsx
$ touch src/screen/ChildScreen.tsx
store.tsx
import {legacy_createStore, combineReducers, applyMiddleware} from 'redux';
import thunk from 'redux-thunk';
import userReducer from './reducer';
const rootReducer = combineReducers({userReducer});
export const Store = legacy_createStore(rootReducer, applyMiddleware(thunk));
reducer.tsx
import {AnyAction} from 'redux';
import {SET_USER_NAME, SET_USER_AGE} from './actions';
const initialState = {
name: '',
age: 0,
};
function userReducer(state = initialState, action: AnyAction) {
if (action.type === SET_USER_NAME) {
return {...state, name: action.payload};
}
if (action.type === SET_USER_AGE) {
return {...state, age: action.payload};
}
return state;
}
export default userReducer;
actions.tsx
export const SET_USER_NAME = 'SET_USER_NAME';
export const SET_USER_AGE = 'SET_USER_AGE';
export const setName = (name: string) => ({
type: SET_USER_NAME,
payload: name,
});
export const setAge = (age: number) => ({
type: SET_USER_AGE,
payload: age,
});
App.tsx
import React from 'react';
import {View} from 'react-native';
import {Provider} from 'react-redux';
import {Store} from './src/redux/store';
import HomeScreen from './src/screens/HomeScreen';
import ChildScreen from './src/screens/ChildScreen';
export default function App() {
return (
<Provider store={Store}>
<View style={styles.container}>
<HomeScreen />
<ChildScreen />
</View>
</Provider>
);
}
HomeScreen.tsx
import React from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import {useSelector, useDispatch} from 'react-redux';
import {setName, setAge} from '../redux/actions';
export default function HomeScreen() {
const {name, age} = useSelector((state: any) => state.userReducer);
const dispatch = useDispatch();
return (
<View style={styles.container}>
<Text>HomeScreen</Text>
<Text>이름: {name}</Text>
<Text>나이: {age}</Text>
<Button
title="이름변경(이순신)"
onPress={() => {
dispatch(setName('이순신'));
}}
/>
<Button
title="나이변경(30 )"
onPress={() => {
dispatch(setAge(30));
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 50,
},
});
ChildScreen.tsx
import React from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import {useSelector, useDispatch} from 'react-redux';
import {setName, setAge} from '../redux/actions';
export default function ChildScreen() {
const {name, age} = useSelector((state: any) => state.userReducer);
const dispatch = useDispatch();
return (
<View style={styles.container}>
<Text>ChildScreen</Text>
<Text>이름: {name}</Text>
<Text>나이: {age}</Text>
<Button
title="이름변경(홍길동)"
onPress={() => {
dispatch(setName('홍길동'));
}}
/>
<Button
title="나이변경(40)"
onPress={() => {
dispatch(setAge(40));
}}
/>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
marginTop: 50,
},
});
반응형
'Frontend > React' 카테고리의 다른 글
[React Native] props (0) | 2022.10.20 |
---|---|
[React Native] custom font 설정 (0) | 2022.10.17 |
[React Native] scrollview (0) | 2022.10.13 |
[React Native] useState (0) | 2022.10.13 |
[React Native] state (0) | 2022.10.13 |
Comments