2017-11-09 3 views
0

私は、他のすべての画面/コンポーネントに反映する必要がある単画面でバランスを更新するため、reduxを複数の画面で表示するようにしています。反応ネイティブのReduxエラー

私はかなり還元しています。あなたはreduxの周りの複雑さを知っているので、それを実装することさえ困難です。

私はGitHubとyoutubeのいくつかの例に続き、それを実装し始めました。

アクションフォルダがあります。次の2つのファイル

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; 

} from 'redux'; './reducers/index.js'のimport 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, 
    }, 
}); 

は、私は、configureストアファイルを完了するために、まだです。そして。不思議なんだけど。どこに登録してアクションを送信する必要がありますか?

ボタンクリックでバランスを更新したいのですが、app.js があります。

reduxを理解して実装するように案内してください。より良いフォルダ構造とreduxを実装するためのより良い方法をお勧めします。

答えて

0

ここにはかなり理解できるビットがあります。

基本的なワークフローは、あなたが店のセットアップとReduxの通じ「イベント」の呼び出しの両方を必要とするこれを実現するには(あなたは別のものとして受け取るコンポーネントを持つことができます)

Component Button > Action > Reducer > Component Props > Render 

です。

以下は例です(完璧ではない場合、ここに入力してください)。他のコンポーネントがアクションから値を取得する方法は、が 'connect' HOCを使用するためです。毎回reduxは状態変化を取得し、接続されているすべてのコンポーネントを呼び出します。ここでは、更新された残高を取得し、そのオブジェクトでコンポーネントの小道具を設定するだけの 'mapStateToProps'関数の一部として返します。残高はthis.props.balanceを介してアクセスされ、表示されます。

これは、アクション内でAPIを呼び出して、結果をレジューサーに格納する場合に、より便利になります。接続されたすべてのコンポーネントは、その新しいデータを取得します。

注1:派遣にはredux-thunkミドルウェアしか使用していませんので、私はそれを使用することを許しています。

注2これは簡単な例です。すべての接続コンポーネントが呼び出されるため、アプリの機能が向上すると、オーバーレンダリングを防止する必要があります。私はここでreselectを使用します。

セットアップ

reducers.js

import { combineReducers } from 'redux'; 
import { balanceReducer } from 'balanceReducer'; 
export default combineReducers({ 
    balanceReducer 
}) 

store.js

import { createStore, applyMiddleware } from 'redux'; 
import thunk from 'redux-thunk' 

import combineReducers from './reducers' 

export default function configureStore() { 
    let store = createStore(combineReducers, applyMiddleware(thunk)); 
    return store; 
} 

index.js

import React, { Component } from 'react'; 
import { AppRegistry, View } from 'react-native'; 
import { Provider } from 'react-redux'; 

import configureStore from './store'; 
import Component1 from './component1'; 

const store = configureStore() 

const myapp =() => (
    <Provider store={store}> 
    <View> 
     <Component1 /> 
    <View> 
    </Provider> 
) 

AppRegistry.registerComponent('myapp',() => myapp); 

コンポーネント

component1.js(キー部分が接続HOCある)

import { connect } from 'react-redux'; 
import React, { Component } from 'react'; 
import { Text, View, TouchableOpacity } from 'react-native'; 
import { updateBalance } from './action'; 

class Component1 extends Component { 

    _updateLedger =() => { 
     this.props.updateBalance(500); 
    } 
    render() { 
     const { balance } = this.props; 
     return (
      <View> 
      <TouchableOpacity onPress={this._updateLedger}> 
       <Text>Update balance</Text> 
      </TouchableOpacity> 
      <Text>{balance}</Text> 
      </View> 
     ) 
    } 
} 

function mapStateToProps(state) { 
    return { 
     balance: state.balanceReducer.balance 
    } 
} 

function mapDispatchToProps(dispatch) { 
    return { 
     updateBalance = (balanceInfo) => dispatch(updateBalance(balanceInfo)) 
    }; 
} 

export default connect(
    mapStatetoProps, 
    mapDispatchToProps 
)(Component1) 

export function updateBalance(balanceInfo) { 

    return { 
     type: types.LEDGER_BALANCE, 
     payLoad: { balanceInfo } 
    } 
} 

balanceReducer.js(ここで重要な部分は、新しい状態を戻すことである)

をaction.js
const initialState = { 
    balance: 0, 
} 
export function balanceReducer(state = initialState, action) { 
    if(action.type === types.LEDGER_BALANCE) { 
     return { 
      balance: action.payLoad.balanceInfo 
     } 
    } 
} 
関連する問題