Skip to content
This repository has been archived by the owner on Apr 1, 2021. It is now read-only.

On componentDidMount, Binder doesn't have any child element #103

Open
ValentinJS opened this issue Nov 24, 2020 · 0 comments
Open

On componentDidMount, Binder doesn't have any child element #103

ValentinJS opened this issue Nov 24, 2020 · 0 comments

Comments

@ValentinJS
Copy link

ValentinJS commented Nov 24, 2020

When mounting a new Binder component, we sometimes want it to be focused on a specific element on initialization.

We tried to use the componentDidMount lifecycle method of the parent component, with a activeBinder(binderId, desiredEltId) but this doesn't seem to be working.

It appears that the activeBinder function will call computeResetBinder and this function will perform a check on the length of the children elements array length.
And this length is initialized at empty array, as can be seen in the buildBinderFromProps function.

Shouldn't the elements be filled with props.children in the buildBinderFromProps function ?
So that the activeBinder function would actually activate it on the specified element ?


Right now, we have to either do this in a setTimeout(..., 0) as it is done in the componentDidMount function of the Binder component.
i.e :

componentDidMount() {
  /* Workaround to make the desiredId focused by default */
  window.setTimeout(() =>
    activeBinder(binderId, desiredId);
  );
}

A less hacky way we found is to do it in the componentDidUpdate function, by first checking if the binder has a selectedId.

i.e :

componentDidUpdate() {
  if (!this.props.selectedId) {
    activeBinder(binderId, desiredId);
  }
}
// SelectedId is coming from the Binder's store
connect(() => ({
  selectedId: getCurrentSelectedId()(),
}))(MyComponent);

So what is the best way to handle this ?

Bonus question : could it be possible to give the desiredElementId to focus as an inline react props ?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant