score:0
There are a few ways of doing this.
Lets take a look at the simplest one - state/props drilling
.
Say you have 2 components - A
and B
in such relation that A
is a parent to B
.
If you want to pass data from A
to B
, you simply pass it through props
. If you want to pass data from B
to A
however, you need to pass a set method
as a property and then call that set method
from B
to set the state of A
:
const A = props => {
const [data, setData] = useState({})
return <B setData={setData} />
}
const B = props => {
return (<button onClick={props.setData("Data")}>Set Data!</button>)
}
If you have a more complex structure, you try to find the closest common parent (component which parents both A
and B
). This however goes only so far, at some point it becomes impossible to maintain and costly to perform, so you will need to apply some sort of global state management
tool. Redux
is the most popular choice, but Context API
is easier to learn.
Source: stackoverflow.com
Related Query
- How to pass json data from one component to another using functions in react
- How to pass data from one component to another component in onchange using React js
- How to pass data from one component to another in React or React-Redux?
- How to pass data from one component to another while using API in reactjs
- How to pass a Json data from one folder to another folder in react
- How to pass a prop from one component to another when using react router link
- how to pass the data from one component to another in react js
- How can I pass props from one component to another using Link and router in react
- How to transfer data from one parent component to another parent component in react using router / router params
- React - How can I pass API data from one component to another in a different js file?
- How do I pass data from one sibling to another using React Router?
- React couldn't pass a data from one class component into another using Link
- How to pass data from one component to another component/parent in React
- How can i pass data from one component to another using hooks
- How to pass value from one component to another using onClick in React
- How to pass props from one component to another using optional handling in react hooks
- How to pass data from a page to another page using react router
- How to pass data from one component to another ReactJS
- How to refresh data of one component from another component in react js
- React - how to populate one select menu based on a value of another select menu using data from a local .json file
- How to pass data from blade.php file to a react component using HTML element attributes?
- React | pass form data from one component to another
- In React How to pass object using history.push from a component to child component and read data
- How to navigate from one component to another by clicking a button using React router
- How to fetch data from API and then pass it to another component in react after it is fully loaded?
- Passing data from one component to another in React using formik
- How to pass data from one variable in props to another in React app?
- How to pass data from one component to another via a function in ReactJS?
- How to pass an array of data to one component to another component using history.push in ReactJs when fetched using Axios
- how to pass data from one container to another container in react router
More Query from same tag
- npm start not working | Not Loading React Application
- Nested menu using Material UI (4.9.2) - React Js (16.8.5)
- Why can't I set an array to API data using useState()?
- How to add active class on click event in custom list group?
- React and FLUX - Where would I implement server routines in my web app?
- react-router V6 "url changes but component does not render"
- Variable or React State?
- Any way for React in a popup to communicate with parent window?
- In Flux/React.js, where to initialize jQuery plugins?
- <Redirect issue with a signup in React.js + Node (express) + MongoDB
- Passing a callback function with arguments coming from different components as a prop
- How to filter city as per selected state in reactjs?
- Returning html from fetch() and showing in to user
- Issue with displaying props in Card Component - React
- React.js Dropdown component doesn't hide on button click
- Getting user location and setting it to state using redux
- could not find react-redux context value ; ensure component is wrapped in a <Provider>
- Create a transparent backdrop over figure
- why I can not use setState value in my code?
- react-draft-wysiwyg Default Value Style doesn't load
- React Hook Form useWatch not displaying correct value in component
- Jest function call for functional component gives error on stimulate click
- Migrating to react-router v4 - Route with children which requires router props
- React-Select not working when options dont have label/values
- TailwindCSS fixed width property not working on resolution change
- Pass filtered objects from parent component to child component asynchronous
- Custom Style on React-Bootstrap's Dropdown Menu component
- I am unable render component with click a button
- Sidebar allowing for double clicks of urls
- REACT:Help conditionally rendering btn