Redux连接函数错误:只能在函数组件的主体内部调用挂钩

时间:2019-04-17 11:06:09

标签: react-native redux react-redux react-hooks

如果我更改此行

  

让Auth = connect(null,null)(AuthScreen)

与此

  

让Auth = AuthScreen

一切正常。但是,我需要使用redux并收到此错误

  

不变违规:只能在钩子的主体内部调用钩子   功能组件。

     

此错误位于:       在ConnectFunction中(由SceneView创建)       在SceneView中(由SwitchView创建)       在SwitchView中(由Navigator创建)       在导航器中(位于createAppContainer.js:387)       在NavigationContainer中(在App.js:29处)       在提供程序中(在App.js:28)       在App中(在renderApplication.js:34)       在RCTView中(在View.js:45)       在View中(在AppContainer.js:98)       在RCTView中(在View.js:45)       在View中(在AppContainer.js:115)       在AppContainer中(位于renderApplication.js:33)

     

此错误位于:       在NavigationContainer中(在App.js:29处)       在提供程序中(在App.js:28)       在App中(在renderApplication.js:34)       在RCTView中(在View.js:45)       在View中(在AppContainer.js:98)       在RCTView中(在View.js:45)       在View中(在AppContainer.js:115)       在AppContainer中(位于renderApplication.js:33)

let Auth = connect(null,null)(AuthScreen);

const RootStack = createSwitchNavigator(
  { Auth:  Auth},
  {initialRouteName: 'Auth'}
);

let AppContainer = createAppContainer(RootStack);

export default class App extends Component<Props> {
  render() {
    return (
        <Provider store={store}>
            <AppContainer />
        </Provider>
    );
  }
}

package.json

    {
  "name": "untitled16",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node node_modules/react-native/local-cli/cli.js start",
    "test": "jest"
  },
  "dependencies": {
    "npm": "^6.9.0",
    "prop-types": "latest",
    "react": "^16.8.3",
    "react-dom": "^16.8.3",
    "react-native": "^0.58.4",
    "react-native-gesture-handler": "^1.1.0",
    "react-navigation": "^3.8.1",
    "react-redux": "^7.0.2",
    "redux": "^4.0.1",
    "redux-thunk": "^2.3.0"
  },
  "devDependencies": {
    "@babel/core": "7.4.3",
    "@babel/runtime": "7.4.3",
    "babel-jest": "24.7.1",
    "jest": "24.7.1",
    "metro-react-native-babel-preset": "0.53.1",
    "react-test-renderer": "16.8.3",
    "redux-devtools": "^3.5.0"
  },
  "jest": {
    "preset": "react-native"
  }
}

AuthScreen.js

class AuthScreen extends Component{
    render(){
        return(
            <Text>This is a text</Text>
        ) }}

export default AuthScreen

0 个答案:

没有答案