2017-07-22 15 views
0

Reduxストアがその値を更新した後、コンポーネントの状態計算プロパティが更新されないのはなぜですか?Reduxストアがその値を更新した後、コンポーネントの状態計算プロパティが更新されないのはなぜですか?

私はいくつかのヘルパーメソッドを使用して、AppStore.getState()を介してサブストアを取得しています。私のisAuthenticated状態プロパティのApiStore。このストア値が更新されると、コンポーネントの状態値は更新されないようです。 React Nativeは計算されたコンポーネントの状態プロパティにストア更新を監視しませんか?

私のコンポーネントは、以下のようになります。

// Vendor 
import React, { Component } from 'react' 
import { AppRegistry, Text, View, StyleSheet, TextInput, Button} from 'react-native' 
import AppStore from './Stores/AppStore' 
import StoreHelpers from './Stores/StoreHelpers' 

// Custom 
import Login from './Components/Login/Login' 
import Api from './Services/Api.js' 

// Styles 
const styles = StyleSheet.create({ 

    mainView: { 
    flex: 1, 
    padding: 20, 
    marginTop: 30, 
    alignItems: 'center', 
    justifyContent: 'center', 
    backgroundColor: '#3b5998', 
    }, 

}); 

// Main App Component 
export default class Main extends Component { 

    constructor(props) { 
    super(props) 
    this.state = { 
     isLoading: false, 
     isAuthenticated: !StoreHelpers.getApiStore().userBalanceResponse.error // Computed property from store 
    } 

    // Enable this for debugging 

    console.log(this.state) 

    AppStore.subscribe(() => { 
    console.log(AppStore.getState()) 
    }) 

    } 

    render() { 
    return (
     <View style={styles.mainView}> 
     <Login /> 
     </View> 
    ) 
    } 
} 

// skip this line if using Create React Native App 
// AppRegistry.registerComponent('AwesomeProject',() => Main); 

答えて

3

コンポーネントがストアに登録されていませんので、あなたはそれを見ることはできません。店舗と関係があるのは、reduxの仕事です。NOT React Nativeです。したがって、あなたのコンポーネントをreact-redux内にラップしてmapStateToPropsに渡して、正しい計算値を取得する必要があります。

// ... rest of imports 
import { connect } from 'react-redux'; 

// Main App Component 
class Main extends Component { 
    constructor(props) { 
    super(props) 
    this.state = { 
     isLoading: false, 
     isAuthenticated: this.props.isAuthenticated, 
    } 

    // ... rest of code 

    } 

    // ... rest of code 
} 

const mapStateToProps = (store) => ({ 
    isAuthenticated: !userBalanceResponse: store.userBalanceResponse.error, 
}); 

export default connect(mapStateToProps, null)(Main); 

あなたが適切Reduxの店をセットアップすることを確認して、それを動作させるために。ルートコンポーネントをプロバイダコンポーネント内にラップし、ストアコンポーネントに渡します。それは、次のようになり、あなたのルートコンポーネントをアプリケーションと呼ばれていると仮定します

import { Provider } from 'react-redux'; 
import { createStore } from 'redux'; 
import Main from 'path-to-main/Main'; 

// we will pass this store to the Provider 
const store = createStore(
    reducer, 
    // ... middlewares etc this is optional 
); 

export default class App extends Component { 
    render() { 
    return(
     <Provider store={store}> 
     <Main /> 
     </Provider> 
    ) 
    } 
} 
+0

これは非常に有益な答えでした。ご協力ありがとうございました。私はまだエラーが発生しています:コンテキストまたは "Connect(Main)"の小道具に「店」を見つけることができませんでした。 にルートコンポーネントをラップするか、明示的に "store"をConnect(Main)の小道具として渡します。私はエラーから最初に提案された解決策を試しましたが、それは助けにはなりませんでした。私は2番目を理解していない。何か案は? – Bob

+0

最初の方法が有効です。おそらくあなたのコードに間違いがあります。私はあなたのコンポーネントに還元店を設定する方法の例を含むように私の答えを更新しました。また、redux docは良い出典です:http://redux.js.org/docs/basics/UsageWithReact.html#passing-the-store –

関連する問題