在React Native上将Redux mapStateToProps集成到App.js中

时间:2019-03-05 18:38:45

标签: react-native redux react-navigation

我想将redux mapStateToPros集成到我的App.js文件中。 为什么?因为当模态出现时我需要阻止模糊效果。 因此,假设我有以下App.js

import React, {Component} from 'react';
import {createStackNavigator, createAppContainer} from 'react-navigation';
import {Provider} from 'react-redux';
import { PersistGate } from 'redux-persist/integration/react'
import store, { persistor } from './src/js/store';
import {createStore, applyMiddleware} from 'redux';

import Reducers from './src/Reducers';

import Preload from './src/screens/Preload';

let store = createStore(Reducers);

const AppNavigator = createStackNavigator({
  Preload: {
    screen: Preload,
  },
});

const AppContainer = createAppContainer(AppNavigator);

export default class App extends Component {
  render () {
    return (
        <Provider store={store}>
          <PersistGate loading={null} persistor={persistor}>
            <AppContainer />
          </PersistGate>
        </Provider>
    )
  }

我需要执行以下操作,但是当我的状态更改时,模糊不会触发。

import React, {Component} from 'react';
import {createStackNavigator, createAppContainer} from 'react-navigation';
import {Provider} from 'react-redux';
import {  findNodeHandle } from 'react-native';
import { PersistGate } from 'redux-persist/integration/react'
import store, { persistor } from './src/js/store';
import {createStore, applyMiddleware} from 'redux';
import { BlurView } from 'react-native-blur';

import Reducers from './src/Reducers';

import Preload from './src/screens/Preload';

let store = createStore(Reducers);

const AppNavigator = createStackNavigator({
  Preload: {
    screen: Preload,
  },
});

onViewLoaded() {
    this.setState({ viewRef: findNodeHandle(this.viewRef) });
}

function mapStateToProps(state) {
  return {
    modalIsOpened: state.modalReducer.modalIsOpened,
  }
}

const AppContainer = connect(mapStateToProps)(createAppContainer(AppNavigator));

export default class App extends Component {
  render () {
    return (
        <Provider store={store}>
          <PersistGate loading={null} persistor={persistor}>
            <AppContainer 
              ref={(viewRef) => { this.viewRef = viewRef; }}
              onLayout={() => { this.onViewLoaded(); }}
             />

            {modalIsOpened && <BlurView
              style={{
                position: "absolute",
                top: 0,
                left: 0,
                bottom: 0,
                right: 0
              }}
              viewRef={this.state.viewRef}
              blurType="dark"
              blurAmount={5}
              blurRadius={5}
            />}
          </PersistGate>
        </Provider>
    )
  }

希望我能说清楚:)

1 个答案:

答案 0 :(得分:0)

您正在将modalIsOpened道具映射到<AppContainer>,但希望它存在于<App>上。

<Blur>内移动<AppContainer>并在其中映射道具。