Reputation: 5932
I am trying to make android application with react native.After creating project and install all dependencies i want to use use Hook inside navigator like this:
const MainNavigator = createStackNavigator({
Dashboard: DashboardScreen,
ListSurveyor: {
screen: topSurvayorsNavigator,
navigationOptions: ({ navigation }) => {
const dispatch = useDispatch();
const [term, setTerm] = useState('');
const searchServeyor = (item) => {
console.log('nav: ', item);
dispatch(survayouActions.searchSurveyouList(item))
}
switch (navigation.state.routes[navigation.state.index]["routeName"]) {
case "ActiveSurveyor":
return {
header: () => <CustomHeader
title='Active'
// term={term}
// onTermChange={setTerm}
onTermSubmit={searchServeyor}
/>
}
case "DeActiveSurveyor":
return {
header: () => <CustomHeader
title='DisActive'
onTermSubmit={searchServeyor}
/>
}
default:
return { title: (navigation.state.routes[navigation.state.index]["routes"])[(navigation.state.routes[navigation.state.index]["index"])].routeName }
}
}
},
Audited: AuditedFileNumSurveyor,
}, {
defaultNavigationOptions: defaultNavOptions
});
I imported useState and useDispatch like below:
import React, { useState } from 'react';
import { useDispatch } from "react-redux";
But When i run application i got this error:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.
I used hook inside method navigationOptions: ({ navigation }) => {
but what is happen?
This is package.json information:
{
"name": "RNMngm",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"base-64": "^0.1.0",
"prop-types": "^15.7.2",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-charts-wrapper": "^0.5.7",
"react-native-gesture-handler": "^1.5.2",
"react-native-paper": "^3.4.0",
"react-native-reanimated": "^1.4.0",
"react-native-svg": "^10.0.0",
"react-native-vector-icons": "^6.6.0",
"react-navigation": "^4.0.10",
"react-navigation-drawer": "^2.3.3",
"react-navigation-header-buttons": "^3.0.4",
"react-navigation-material-bottom-tabs": "^2.1.5",
"react-navigation-stack": "^1.10.3",
"react-navigation-tabs": "^2.7.0",
"react-redux": "^7.1.3",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"@babel/core": "7.7.7",
"@babel/runtime": "7.7.7",
"@react-native-community/eslint-config": "0.0.5",
"babel-jest": "24.9.0",
"eslint": "6.8.0",
"jest": "24.9.0",
"metro-react-native-babel-preset": "0.56.3",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
Upvotes: 0
Views: 2948
Reputation: 57
Hooks can only be used in a component, and cannot be used in navigator or navigationOptions. If u need to use state in navigator or navigationOptions, u need to setParams in the component and then use the getParam method in navigationOptions
Upvotes: 1