score:1
Accepted answer
You could go with solution to have context and have all wrapped components to access it with useContext
hook.
This is how you can use it:
App component
import React, { Component, useState } from 'react';
import Nav from './Nav';
import Main from './Main';
const AppContext = createContext({
executeNavigationFunction: () => {},
setExecuteNavigationFunction: (navFunction: () => void) => {},
});
const App = () => {
const [executeNavigationFunction, setExecuteNavigationFunction] = useState(() => {});
return (
<AppContext.Provider value={{ executeNavigationFunction, setExecuteNavigationFunction }}>
<Nav />
<Main />
</AppContext.Provider>
);
};
export default App;
Nav component
import React, {useEffect, useContext, useEffect} from 'react';
import { NavLink } from 'react-router-dom';
import AppContext from 'app';
const Nav = (props) => {
const { setExecuteNavigationFunction } = useContext(AppContext);
function doSomethingToNav() {
// here's where I want to change appearance or do whatever to the nav
// I want to be able to use this function from within my Nav component
// but also want to be able to call it from any another component
}
useEffect(() => {
setExecuteNavigationFunction(doSomethingToNav)
}, [setExecuteNavigationFunction])
return (
<nav>
<NavLink to="/pageone">Page One</NavLink>
<NavLink to="/pagetwo">Page Two</NavLink>
</nav>
);
};
export default Nav;
PageOneItem component
import React, {useEffect, useContext} from 'react';
import { Link } from 'react-router-dom';
import AppContext from 'app';
const PageOneDetail = (props) => {
const { executeNavigationFunction } = useContext(AppContext);
useEffect(() => {
executeNavigationFunction();
}, []);
return (
<div>
<Link to="/pageone">Close</Link>
// I want to call doSomethingToNav function when I close this component. // Close, in this case, is just routing
back to /pageone
<p>Some content...</p>
</div>
);
};
export default PageOneDetail;
In this way you can use any data from any part of the app in any part of the app.
It may seem a bit hacky, but if you are not using some global state lib, Context is the way to go.
Source: stackoverflow.com
Related Query
- In React, how do I call a function in one component (which is being rendered with React Router) from another component?
- How to test a handle function call with a react functional component with hooks
- How to call a function with arguments onclick of a button in a react component
- How to call a function from different component using React with Redux Form?
- How to make a button onClick in one component call a function in a different component React
- React Bootstrap make only one collapse open up when being rendered with a map function / display relevant information in each collapse
- How to call a function in one React component by button click in another component
- How to call loading function with React useEffect only once
- How do I export more than one class component with React JS?
- How to specify function parameters for React component callback with TypeScript?
- How to mock API calls made within a React component being tested with Jest
- How to use jest.spyOn with React function component using Typescript
- How to replace a component with another one upon event (button click) in react js
- How to call a component function on other component, but from the other component ? React
- react functional component with ag grid cannot call parent function via context
- How to test with jest and typescript with types a basic react function component
- How does one React component call a method in another React component?
- How to replace one React Component with another OnClick?
- How to test styles and media queries rendered by a React component with Jest and/or Enzyme
- How do I test a React component that calls a function on its parent, which changes props on the component?
- Creating A React Component That Can Be Shown With A Function Call (like react-toastify
- How to only trigger one onClick function when inside a component also with onClick event?
- How to define an interface for React function component with defaultProps without throwing missing type error?
- How to call inherited function in react child component
- How to implement a React Component with sub-components and call them?
- How to optimize in react js while working with useReducer, looped over state which is passed to child component
- How to call event from component after setting its state with React hooks?
- How to handle multiple inputs in an array with one function in react
- How to call a React Context function with an argument
- How to test a function inside React functional component using jest.spyOn with React Testing Library
More Query from same tag
- Object appears to be empty inside of react render() even if previous logging proves that it's not
- Infinite Scroll in React JS, but data from Reddit keeps loading and loading
- Changing state of generic functional child component
- React Router isn't working properly
- How to pass parameter in People Picker selecteditem React Control
- I can't map through the array 'products'(React/Redux)
- React Popover not closing
- Create own react route class in typescript
- Recharts Customized XAxis name
- Get/catch response status in a promise
- Using AND operator inside JSX block of code
- Can I use none-pure object as state in React?
- Put calls in a queue and waiting for updating the state in React.js
- Call api before first render in functional component in React.js
- Fetching user details in React from django-rest-auth gives back 403 (Forbidden) - Using authtoken
- Android Back button handler react js
- Get table pagination buttons and rows per page to the left on Material UI
- How to change an array inside state
- reactjs Delete Operation Warning: Failed prop type: The prop `role` is marked as required in `ManageRolePage`, but its value is `null`
- Next-Auth : How the registration is handled with a email + password credential provider?
- How to import a map defined in an external json file?
- React - return component onClick event
- Expand React Bootstrap Table for more granular data
- Webpack Can't Resolve React Components
- setState not triggering a re-render when data has been modified
- removing previous object from array with useState method in react
- Difference between useCallback hook and empty dependency array and just defining a function outside of the component
- Calling one function in another and passing it id, react + redux
- Is There a Way to Dynamically Create a React Component
- How to push values correctly into an array of objects