Share variabel with different components.

I have a component with a usestate hook - const [co2TotalSaving, setCo2TotalSaving] = useState(0), i have a function called calculateCo2 that change this co2TotalSaving that is trigged with a onclick , - function calculateCo2() { const data1co2 = TotalRevenueSeries[0].data.map(value => value * 0.425); const data2co2 = TotalRevenueSeries[1].data.map(value => value * 0.425); const co2savings = data1co2.map((value, index) => value - data2co2[index]); const TotalCo2S = co2savings.reduce((acc, value) => acc + value, 0) / 1000; setCo2TotalSaving(Math.ceil(TotalCo2S)); ; } I need to send this value achieved from the variabel to a another component where i ll do other calculations with that value, the 2 components are totally independent and should continue like that.. Can anyone help me? how do I do this?
No description
3 Replies
sunny-green
sunny-green16mo ago
Hey @brunob.bueno, It seems a React question, maybe the community can help you 🙏
harsh-harlequin
harsh-harlequin16mo ago
Maybe you can use a react context https://reactjs.org/docs/context.html or store it in a store like redux https://redux.js.org/
Context – React
A JavaScript library for building user interfaces
Redux - A predictable state container for JavaScript apps. | Redux
A predictable state container for JavaScript apps.
harsh-harlequin
harsh-harlequin16mo ago
I think a context would be a bit easier