In functional components
I want to share a function I have with a few components, to prevent duplicated code. All I need is the state that the function changes.
Is it possible to get the state from UpdateNum.jsx component to other components (as I showed in the code) ?
Or, is it possible to pass this function to other components so I can use it multiple times ? ( also to send props to the function)
import { useEffect, useState } from 'react';
export default function UpdateNum(change) {
const [numberCon , setNumber ] = useState(0);
useEffect(() => {
setNumber(numberCon + 1)
},[change])
return numberCon;
}
I want to get the numberCon state in Shop.jsx inside the value={} attribute
import UpdateNum from './functions/UpdateNum';
<div className='cart-icon' value={''}>
<FaShoppingCart size={40} />
</div>
And also at Items.jsx as I do -
import UpdateNum from './functions/UpdateNum';
return (
<div >
<h1 style={{ marginLeft: '30px' }}>
<UpdateNum/> - Items <br />
</h1>
</div>
);