하나의 화면에서 균형을 업데이트 할 때 redux를 구현하여 여러 화면에서 균형을 유지하려고합니다. 다른 모든 화면/구성 요소에 반영되어야합니다.반응식 네이티브의 Redux 오류
나는 꽤 새로운 편이다. 당신이 redux 주변의 복잡성을 알고 있기 때문에 심지어 그것을 구현하기 어렵게 만듭니다.
나는 GitHub 및 youtube에서 몇 가지 예를 따라 그것을 구현하기 시작했습니다.
아래 작업 폴더 있습니다. 다음 두 파일
counteractions.js
기어 folder.I에서import * as types from './actionTypes.js';
//ActionCreator methods
export function updateBalance(balanceInfo) {
return {
type: types.LEDGER_BALANCE,
payLoad: { balanceInfo }
}
}
는이 파일이
balance.js ConfigureStore.js
수입 {createStore에서
import * as types from '../actions/actionTypes.js';
const initialState = {
balance: 0
}
// reducer functions .. accepts current/initial state , actions and returns new state
const balanceReducer=(state,action)=>
{
switch (action.type) {
case types.LEDGER_BALANCE:
return {
balance: action.payload.balanceInfo
}
break;
default:
break;
}
}
export default balanceReducer;
}에서 'redux'; './reducers/index.js'에서 가져온 rootReducer;
import balanceReducer from './reducers/balance.js';
const initailState = {
balance: 0,
}
export const store=createStore(balanceReducer,balanceReducer);
App.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import { Provider } from 'react-redux';
//Provider - makes redux store available to connect() class in component hierarchy below
import { applyMiddleware, createStore, compose, combineReducers } from "redux";
import thunkMiddleware from 'redux-thunk';
import createLogger from 'redux-logger';
import rootReducer from './reducers/index.js';
//import store from './configureStore.js';
import {
Platform,
StyleSheet,
Text,
View,
TouchableOpacity,
TextInput
} from 'react-native';
import ReduxDemo from "./reduxDemo.js";
import { store, reducer } from './balanceDemo.js';
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' +
'Cmd+D or shake for dev menu',
android: 'Double tap R on your keyboard to reload,\n' +
'Shake or press menu button for dev menu',
});
export default class App extends Component<{}> {
constructor(props) {
super(props);
this.state = {
balancelocal: '',
}
}
_updateLedger =() => {
// store.dispatch({ type: 'BALANCE', payLoad: '500' });
store.dispatch({ type: 'BALANCE', payLoad: 'Your balance is 8000 MUR' });
}
render() {
store.subscribe(() => {
this.setState({
balancelocal: store.getState(),
})
//this.balanceInfo = store.getState().balance;
// alert(this.state.balancelocal);
});
return (
<View style={styles.container}>
<TouchableOpacity onPress={this._updateLedger}>
<Text>Update balance</Text>
</TouchableOpacity>
<TextInput style={{height:100,width:400}} value={this.state.balancelocal}/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
나는 구성 저장소 파일을 완료하는 데 아직입니다. 과. 궁금하네요. 어디에서 구독하고 조치를 파견해야합니까?
버튼을 클릭하여 균형을 업데이트하고 싶습니다. app.js 있습니다. 자동으로 다른 페이지의 잔액을 업데이트하십시오 ..
redux를 이해하고 구현하도록 안내해주십시오. 더 나은 폴더 구조와 redux 구현 방법을 제안하십시오.