score:1
Accepted answer
Accessing the
React hooks can only be called in function components or custom React hooks. In order to use the navigate
function you'll need to either create a wrapper component or a custom withNavigate
HOC to pass navigate
as a prop to your class component.
Wrapper
const MyComponentWithNavigation = props => {
const navigate = useNavigate();
return <MyClassComponent {...props} navigate={navigate} />;
};
Usage:
<Route path="..." element={<MyComponentWithNavigation />} />
HOC
const withNavigate = Component => props => {
const navigate = useNavigate();
return <Component {...props} navigate={navigate} />;
};
Usage:
const MyComponentWithNavigation = withNavigation(MyClassComponent);
...
<Route path="..." element={<MyComponentWithNavigation />} />
Accessing the navigate
function
In your class component, access navigate
from props.
this.props.navigate("/");
Source: stackoverflow.com
Related Query
- React call from class to functional component. Error: Invalid hook call. Hooks can only be called inside of the body of a function component
- Invalid hook call error when using React function component from npm package
- Hook error using Truffle React box (transforming the App.js from Class component to Functional component)
- React hooks in react library giving Invalid hook call error
- Cant use Hooks with my React Component Library. Invariant Violation: Invalid hook call
- React 16: Call children's function from parent when using hooks and functional component
- Importing and using component from custom React Component Library results in Invariant Violation: Invalid hook call
- Converting class component to functional component using react hooks (useRef)—how to avoid "undefined" error
- Using dispatch in class based component give error of invalid hook call
- Invalid hook call. Hooks can only be called inside... how to fix this maintaining the react class component
- How to call a react hook fetch request in a functional component to access data then pass to a class component to map?
- Invalid hook call in class component when I try to use useParams to get the param from url
- React - Invalid hook call & class Component
- Getting Invalid hook call error on importing a component in React
- React Redux-Toolkit: Invalid hook call - Hooks can only be called inside of the body of a function component
- Got Error: Invalid hook call when trying to call ReactToPrint from another functional component
- Getting invalid hook call error in React when I used materidal design bootstrap component in my application
- How to call service class method from functional component in react
- Invalid hook call error when showing/hiding component with React Redux
- I have this React Invalid Hook Call Error that I'm getting and I wanted to see if I was breaking the rules of hooks or is it something else?
- converting onCancel to functional component with react Hooks from class component
- Converting class to function React Error invalid hook call
- Reconstructing from class to functional hook component in react
- Invalid hook call. Hooks can only be called inside of the body of a function component when apply style to class base component with material-ui
- React useContext throws Invalid hook call error
- React Hooks won't work on Firebase Cloud Functions Error: Invariant Violation: Invalid hook call
- React hooks in Gatsby: Invalid hook call
- React + Antd + Rollup Component Library "Error: Invalid hook call. Hooks can only be called inside of the body of a function component"
- React 17.0.1: Invalid hook call. Hooks can only be called inside of the body of a function component
- React invalid hook call error and storybook 6
More Query from same tag
- React axios POST file missing in Laravel Request
- React Router redirects all requests to "/"
- try to render todo item as <li>, but todo.name is undefined
- Toggle icon color upon button click using React Hooks
- Keyboard event event.key is unidentified on android webview
- ES6 isomorphic-fetch does not send headers
- How to cast type on imported react component?
- Trouble getting variables out of the URL
- pass props between react components
- const or let in React component
- What are the advantages of useRef() instead of just declaring a variable?
- React PropTypes with multiple level object
- Replace string with JSX element?
- Why axios.get doesn't work when I use redux?
- Accessing json data entry using props gives error: undefined is not an object
- Using Internet explorer to run React JS apps
- Office Fabric in SharePoint Framework: how to disable icon warnings?
- Nested Routes not rendering with React Router v4
- Why id variable is coming undefined in my react code in todo list Delete component?
- React Render a Component in two places
- Add an additional property and value to a state object when transfering from one state to another. [Hooks]
- Populate index value in column from the data in typescript(Angular/React)
- React JS error: this.state.data.map is not a function
- How can I get the href id from a react route?
- Render HTML string w/JSX expression in React dangerouslySetInnerHTML()
- Get scroll position with Reactjs
- muiTheme` was not specified in `Toolbar`
- Change the state based on the previous state with React Hooks
- How to submit a form with react and redux?
- Working with Audio objects in React/ Redux