Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent behavior on conditional render on mobile #740

Open
peter1357908 opened this issue Feb 2, 2024 · 0 comments
Open

Inconsistent behavior on conditional render on mobile #740

peter1357908 opened this issue Feb 2, 2024 · 0 comments

Comments

@peter1357908
Copy link

If I have a component that looks like this:

const Foo = () => {
  const [state, setState] = useState(null);
  const renderBar = () => {
    if (state) { return (<div>Baz 1</div>); }
    return (<div>Baz 2</div>);
  }
  return (
    <Draggable>
      {renderBar()}
    </Draggable>
  );
}

Then there is a chance that when either Baz 1 or Baz 2 gets rendered, it's not draggable. I suspect that this has to do with the state changing and Draggable only applying to whichever gets the initial render.

This observation comes from about 2 hours of debugging... for context, in my project, state is actually passed into the Foo component and can be set outside of Foo. Baz 2 is a table of a lot of components, whose slow rendering may or may not have contributed to this inconsistent behavior.

I resolved the inconsistency by individually wrapping Baz1 and Baz2 like this:

const Foo = (props) => {
  const [state, setState] = useState(null);
  const renderBar = () => {
    if (state) { return (
      <Draggable>
        <div>Baz 1</div>
      </Draggable>
    ); }
    return (
      <Draggable>
        <div>Baz 2</div>
      </Draggable>
    );
  }
  return renderBar();
}

Is this inconsistency specific to react-draggable or is it an expected behavior of react?

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

No branches or pull requests

1 participant