score:2
Accepted answer
In React, You can pass the data two way most commonly:
1.
- Pass a callback function to from common main to child component.
- When async action is done, execute it with data.
- Give it the data with props to another component.
export default function Bucket({ onGetData }) {
const { slug } = useParams();
const classes = useStyles();
const [data, setData] = useState({ bucket: [] });
useEffect(() => {
axiosInstance.get('bucket/' + slug + '/').then((res) => {
setData({ bucket: res.data });
console.log(res.data);
});
}, [setData, slug]);
const getData = () => {
axiosInstance
.get('bucket/fin-data/' + slug).then((response) => {
onGetData(response);
console.log(response)
})
}
return (
<Container component="main" maxWidth="md">
<CssBaseline />
<div className={classes.paper}></div>
<div className={classes.heroContent}>
{...}
<button onClick={getData}>get data</button>
</div>
</Container>
);
}
export default function MainComponent() {
const [data, setData] = useState({});
const onGetData = (result) => {
setData(result);
};
return (
<MainComponent>
<Bucket onGetData={onGetData} />
<BarChart data={data} />
</MainComponent>
);
}
- You can pass with any state Manager: Redux, Mobx e.g.
Source: stackoverflow.com
Related Query
- React - How can I pass API data from one component to another in a different js file?
- 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 fetch data from API and then pass it to another component in react after it is fully loaded?
- How can I filter json data from api call in react into different sections of the component
- 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 pass data from one component to another component in onchange using React js
- 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 json data from one component to another using functions in react
- How can I use response data from an API to call another different API request in React UseEffect?
- how can i pass data from one component into another component in react.js
- How to send specific API data from one component to another called within React Component in react
- How to pass data from one component to another ReactJS
- How to refresh data of one component from another component in react js
- React | pass form data from one component to another
- How to pass state/data from one component to another in React.js (riot api specifically)
- How can i pass a state from one react route to another react route?
- How can I push my react form data to a dynamic API query in another component
- 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 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 can I pass the state from one component to another and how can I modify the state?
- how to pass data from one container to another container in react router
- How to pass state from one component to another in React js?
- React pass fetched data from API to another component
- How to transfer data from one parent component to another parent component in react using router / router params
- How to pass data received from a database as props to another component in react
More Query from same tag
- React: Updating state in useEffect hook
- How to remove duplicate slashes from URL in Next Js.How to intercept and modify a request on the server side getServerSideProps?
- Interface Naming Convention (Interface Name clashes with Component Name)
- Why are my dimensions not updating on resize when using React Hooks
- I cannot get React Transition Group to work. No enter or leave transitions
- How can I complete remove menu, toolbar from editor using React-TinyMCE
- How can I get the current time using timezone offset using moment.js?
- How to clear react-select values on button click without adding it to options?
- React, printer friendly printable area to print (Ctrl+P)
- Styles not inserted into HEAD using Webpack MiniCssExtractPlugin with Create React App
- Getting back the value of a `cleared` input element with React Select
- Handling with event input and debounce function
- dataTable cols don't span to the correct width (dc.js,crossfilter, d3 v4.2.8,React)
- Storing and calling functions in React Context leads to weird behavior
- Why is my React Text component showing no text? (React 360)
- React bootstrap open-iconic showing a 'T' symbol instead of the relevant icon
- Updating state with React does not update DOM in IE9
- Saving state from child component by pressing a button in parent component?
- mapping over items in array - react
- Pass props to other component ReactJS
- React handle error if user enters incorrect query format into search bar
- antd autocomplete dropdown height
- How to render a nested component with react-router
- Best way to submit FORM data with React Redux?
- Webpack: Trying to expose a bundled object to be usable by other scripts, object is still undefined
- How can I display my API response message on my frontend UI with React?
- React - Nested routes - Get depth of current URL
- integration test for axios fetching, and it fails when I apply the `done` as callback in the `it` function with jest
- handleuserinput in react. How to concatenate [e.target.name] to another string
- Using react-mapbox-gl (react) to make 'search this area' of google