I have a component, which arranges elements in a dynamic grid, something like this:
class GridComponent extends React.Component {
render() {
return <div>
{items.map(function(item){
return <ItemComponent someData={item}/>;
})}
</div>
}
}
Now I want to position the ItemComponents
based on some algorithm, which needs the individual ItemComponents
dimensions.
So I guess, I need to:
ItemComponents
ItemComponents
(which are only fixed after they have been rendered)ItemComponents
based on my algorithmSo my question is how to this, or more specific:
ItemComponents
have been rendered?ItemComponents
from within GridComponent?GridComponent
with the calculated ItemComponents
position or should I set the positions of the ItemComponents
somehow directly?The size of a component is determined by the height and width of the container. It can be determined if we assign a ref to that component. The useRef function with ref attribute are used to get the current size of the component.
You can place a ref on the container of the floating divs and get the height of the div via the innerHeight property whenever data/sizes change. React Ref Docs. innerHeight can be used for document.
To get the parent height and width in React: Set the ref prop on the element. In the useEffect hook, update the state variables for the height and width. Use the offsetHeight and offsetWidth properties to get the height and width of the element.
To pass data from child to parent component in React:Pass a function as a prop to the Child component. Call the function in the Child component and pass the data as arguments. Access the data in the function in the Parent .
That's a pretty cool idea and you can totally do this using internal state and a few React lifecycle methods. To answer each of your questions:
componentDidMount
will get called on the parent after all constructors and componentDidMount
of every child, therefore here is a good place to call some function that maybe relies on your stateItemComponent
s therefore on componentDidMount
in ItemComponent
, you can set a ref
and find the width and height of the DOM elementItemComponent
can have a prop called initialized
which is false until the parent finds where to place it.Putting all of these together, since your algorithm will likely need all dimensions to work, you'll likely want to create a callback in your parent, let's call it setItemDimensions(id, width, height)
, which gets called on componentDidMount
of each ItemComponent
. You should be keeping a map of all your initialized ItemComponent
s and every time the setItemDimensions
is called, check to see if there are any remaining (maybe initialize the map with all nulls and assume it's "ready" when there are no nulls left).
Once the last null
is gone, you can run the algorithm, figure out the positions, and render the ItemComponent
s in the right place and with initialized={true}
(or just initialized
).
Let's see if that works!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With