I want to recovery the DOM.offsetWidth of all childrens. All of them have different sizes, and depends of style theme based on css.
Everything works fine on ComponentDidUpdate() method, but this is not triggered at first time.
I Can not put all the code, but below the example code, so:
- How to wait to all childrens? or
- How to tell to parent: All DOM is ready, now check the size
Example Parent
class Parent extends Component {
this.childDOMRef = null;
setChildDom(element) {
this.childDOMRef = element;
}
componentDidMount(){
const childrenWidth = [...this.childDOMRef.children]
.reduce((prev, current) => prev + current.offsetWidth, 0);
console.log(childrenWidth); // :( it is 0 on DidMount
}
render(){
return <ChildStateLess
items={this.props.items} // I receive from redux
setInnerRef={this.setChildDom}
>
}
}
Example Child (stateless)
const ChildStateLess = ({setInnerRef, items}) => {
<div ref={setInnerRef}>
{ items.map(item) => (<div>{item}</div>)}
</div>
};
/// UPDATE
OnDidMount obviously the child items does not exist.
OnDidUpdate all exists, but this method is not triggeren on enter to web.
/// UPDATE 2
Keep a number reference, for last rendered, does not work as expected can not get DOM property