从提供者App.js反应本机调用操作

Ala*_*nIb 0 react-native redux react-redux

我使用react native和redux作为以下(工作):

App.js

export default class App extends React.PureComponent {
  constructor (props) {
    super(props);

    this.state = { ...store.getState()};
    store.subscribe(() => {
      // i can read the state but no write it
      const storestate = store.getState();       
    });
  }

  handleConnectivityChange = isConnected => {
    this.setState({ isConnected });
  };

  render() {
    return (
      <Provider store={store}>
        <PersistGate
          loading={<ActivityIndicator />}
          persistor={persistor}>

          <View style={{ flex: 1, marginTop: Config.marginTop }}>
            <StackView />

          </View>
        </PersistGate>
      </Provider>
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

ScreenX.js

import * as actions from "./redux/actions";
import { connect } from "react-redux";

class ScreenX extends React.Component {
    ...
    // i can do in any fonction 
    this.props.action_saveNetInfo(isConnected);
    ...
}

function mapToStateProps(data) {
  return {
    isConnected: data["isConnected"] 
  };
}
export default connect(mapToStateProps, actions)(ScreenX);
Run Code Online (Sandbox Code Playgroud)

当我从App.js以外的其他组件调用动作时,redux存储工作很好

问题:

我想从APP.js调用操作,但这不起作用

  handleConnectivityChange = isConnected => {
    this.props.action_saveNetInfo(isConnected);
  };
Run Code Online (Sandbox Code Playgroud)

错误: TypeError: this.props.action_saveNetInfo is not a function

我也不能这样做

  class App extends React.PureComponent { ...   }

  export default connect(null, null)(App)
Run Code Online (Sandbox Code Playgroud)

因为它抛出一个错误:

    Invariant Violation: Could not find "store" in either the context or props of "Connect(App)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(App)".
Run Code Online (Sandbox Code Playgroud)

任何的想法 ?谢谢

Dio*_*llo 5

在App组件中,您可以dispatch直接从中调用store.然后,您只需要发送您的actionCreator.

import {action_saveNetInfo} from './redux/actions'
// ...
handleConnectivityChange = isConnected => {
    store.dispatch(action_saveNetInfo(isConnected));
};
Run Code Online (Sandbox Code Playgroud)