I have an object state priceRanges in component A , which i initialize with a key and a value:
const [priceRanges, setPriceRanges] = useState({
[randomId]: {
"price": 0,
"quantity": 0
}
})
And then pass it through props to component B:
<ComponentB pricesRanges={pricesRanges} ...props />
The problem is that in Component B , I am converting this object into an array on useEffect:
React.useEffect(() => {
const getArrayData = () => {
const keys = Object.keys(priceRanges)
keys.map(key => {
console.log(priceRanges[key]["price"])
priceRangesArray.push({
id: key,
price: priceRanges[key]["price"] ? priceRanges[key]["price"] : 0,
quantity: priceRanges[key]["quantity"] ? priceRanges[key]["quantity"] : 0
})
})
}
getArrayData()
}, [priceRanges])
But the component does not rerender after the useEffect has run. As such, the component does not render the initial state that pricesRanges was initialized with. To test this, I did this:
{priceRangesArray.length > 0 ?
(priceRangesArray.map(quantityAndPrice => {
return (
<div className="flex" key={quantityAndPrice.id}>
{console.log("I AM RENDERING")}
<input
...props
/>
<input
...props
/>
</div>
)
})) :<p>Hello</p>}
Which always renders Hello on mount . How can I tackle this ? Thanks you !
ComponentBwhich will be calculated frompriceRangesin youruseEffect