17

react-nativeに以下のreduxコンフィグレーションがあります:react-native-router-fluxredux-persistです。リフレッシュ時に最新の現在のルートを取得したいのですが、ルートスタックがリロード時に上書きされています。ルータ状態がレフィックスと反応ネイティブで維持されていません

これはreducers/index.jsファイル

import { combineReducers, createStore, applyMiddleware, compose } from 'redux' 
import { persistStore, autoRehydrate } from 'redux-persist' 
import { AsyncStorage } from 'react-native' 
import logger from 'redux-logger' 

import { startServices } from '../services' 
import navigation from './navigation' 
import devices from './devices' 
import rooms from './rooms' 


import { ActionConst } from 'react-native-router-flux' 

function routes (state = {scene: {}}, action = {}) { 
    switch (action.type) { 
    // focus action is dispatched when a new screen comes into focus 
    case ActionConst.FOCUS: 
     return { 
     ...state, 
     scene: action.scene, 
     }; 

    // ...other actions 

    default: 
     return state; 
    } 
} 

export const reducers = combineReducers({ 
    routes, 
    navigation, 
    devices, 
    rooms 
}) 

const middleware = [logger()] 

const store = createStore(
    reducers, 
    compose(
    autoRehydrate(), 
    applyMiddleware(...middleware) 
) 
) 

persistStore(store, {storage: AsyncStorage}, function onStoreRehydrate() { 
    startServices() 
}) 

export { store, reducers } 

EDITです:これはそれでindex.jsは、プロバイダやシーンですされています

import React, { Component } from 'react' 
import { store } from './reducers' 
import { Provider, connect } from 'react-redux' 
import { Router, Scene, Actions } from 'react-native-router-flux'; 

import Home from './containers/home' 
import Login from './containers/login' 
import Device from './containers/device' 


const scenes = Actions.create(
    <Scene key="root"> 
     <Scene key="home" component={Home} /> 
     <Scene key="login" component={Login} /> 
     <Scene key="device" component={Device} /> 
    </Scene> 
) 

const RouterWithRedux = connect()(Router) 

export default class EntryPoint extends Component { 
    render() { 
    return (
     <Provider store={store}> 
     <RouterWithRedux scenes={scenes} /> 
     </Provider> 
    ) 
    } 
} 

答えて

0

react-native-router-flux reduxと一緒に使用しても、シーンを単独で復元することはできません。 Reduxは状態を追跡するだけなので、ナビゲーション状態を維持したい場合は、起動時にアプリを前のシーンにナビゲートする必要があります。

あなたはreact-native-router-fluxで作業していると仮定して、あなたの状態を得るためにあなたのアプリの最初のコンポーネントをreduxに接続してから、シーンを一致させるだけです。あなたができることをコンポーネントのあなたのライフサイクルのいずれかの方法で

const mapStateToProps = (state) => { 
    const { nav } = state 

    return { 
    currentScene: nav.scene.name, 
    } 
} 

INITIAL_COMPONENT = connect(mapStateToProps)(INITIAL_COMPONENT) 

その後:

だから、あなたのアプリのためにロードされて、あなたの最初のコンポーネントのシーンで、あなたの再来ストアへのアクセスを得るために、最後にこのような何かを行いますそのようにリダイレクト:

const { currentScene } = this.props 
const initialScene = "Login" 

if (currentScene !== initialScene) { 
    Actions[currentScene]() 
} 

ます。また、最後のシーンに移動するときに使用された小道具を渡すか、Reduxの店はそれが永続化だというシーンを持っていない場合、あなたが行きたいデフォルトのシーンを設定することができます。私が取り組んでいるアプリは今美しく状態を持続させています。

+0

最初のシーンのComponentWillUpdateで実行する必要があります。これにより、最後にアクセスしたシーンに遷移する前に最初のシーンが表示されます。それを避ける方法はありますか? – Avery235

+0

私はそれを避ける方法を見つけていませんが、これまでに実行したシナリオのほとんどはそれほど長くはありません。私はその間に代替案を探し続けるつもりです。 –

0

あなたはこのようなあなたのstoreを作成する必要があります。

const store = createStore(
    reducers, 
    compose(
    applyMiddleware(...middleware), 
    autoRehydrate(), 
) 
); 

autoRehydrateはあなたのcomposeの一部である必要があります。 this compose exampleを確認してください。

+0

私も実際にそれを試しました。 'autoRehydrate'はエンハンサーと同様に動作します。 – jsdario

+0

私はこれで遊べるレポを持っていますか? – rclai

+0

申し訳ありません共有するパブリックレポを持っていません – jsdario

0

あなたはこれを試すことができます私は何が起こることはCREATESTORE

const enhancers = compose(
    applyMiddleware(...middleware), 
    autoRehydrate(), 
); 

// Create the store with the (reducer, initialState, compose) 
const store = createStore(
    reducers, 
    {}, 
    enhancers 
); 

またはその他のソリューションはimport {REHYDRATE} from 'redux-persist/constants'で手動でやるべきことのためだと思うし、あなたの減速の内側にそれを呼び出します。

関連する問題