React native 从提供者App.js响应本机调用操作
我使用react native和redux如下(工作): App.jsReact native 从提供者App.js响应本机调用操作,react-native,redux,react-redux,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 i
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>
);
}
}
当我从App.js以外的组件调用操作时,redux商店工作得很好
问题:
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>
);
}
}
我想从APP.js调用操作,但这不起作用
handleConnectivityChange = isConnected => {
this.props.action_saveNetInfo(isConnected);
};
错误:TypeError:this.props.action\u saveNetInfo不是一个函数
我也不能这样做
class App extends React.PureComponent { ... }
export default connect(null, null)(App)
因为它会抛出一个错误:
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)".
不变冲突:在“连接(应用)”的上下文或道具中找不到“存储”。将根组件包装在中,或者将“store”作为道具显式传递给“Connect(App)”。
有什么想法吗?
感谢使用mapDispatchToProps函数将操作绑定到此。道具使用mapDispatchToProps函数将操作绑定到此。在应用程序组件中,您可以直接从
存储中调用调度。然后,您只需要发送actionCreator
import {action_saveNetInfo} from './redux/actions'
// ...
handleConnectivityChange = isConnected => {
store.dispatch(action_saveNetInfo(isConnected));
};
在应用程序组件中,您可以直接从商店
调用调度
。然后,您只需要发送actionCreator
import {action_saveNetInfo} from './redux/actions'
// ...
handleConnectivityChange = isConnected => {
store.dispatch(action_saveNetInfo(isConnected));
};
导出默认连接(null,null)(应用)
抛出错误。connect的第二个参数是mapDispatchToProps或action。我使用actionmapDispatchToProps=dıspatch=>{//返回一些操作;}导出默认连接(mapToStateProps,mapDispatchToProps)(Something)
这样使用。mapDispatchToProps=>Thx的示例,这是通常使用的(action=mapdispatch)。如果我在应用程序中使用connect(无论我给出什么参数),它都会抛出错误。请查看Diogo Sgrillo的响应,它会工作。您的工作来自应用程序以外的其他组件。sagol导出默认连接(null,null)(应用)
抛出错误。connect的第二个参数是mapDispatchToProps或action。我使用actionmapDispatchToProps=dıspatch=>{//返回一些操作;}导出默认连接(mapToStateProps,mapDispatchToProps)(Something)
这样使用。mapDispatchToProps=>Thx的示例,这是通常使用的(action=mapdispatch)。如果我在应用程序中使用connect(无论我给出什么参数),它都会抛出错误。请查看Diogo Sgrillo的响应,它会工作。您的工作来自应用程序以外的其他组件。萨戈尔