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

Add a condition property to the Mount component #24

Merged
merged 2 commits into from
Jan 19, 2022
Merged

Conversation

creativecreature
Copy link
Contributor

@creativecreature creativecreature commented Jan 5, 2022

Overview

With these changes I hope to make it easier, and slightly more efficient, to perform conditional rendering.

Sometimes I've had to derive two boolean facets like this:

const itemsFacet = useFacetMap((facet) => facet.items, [], [useRemoteFacet(someFacetSelector)])
const hasItems = useFacetMap((facet) => Boolean(facet.items.length), [], [itemsFacet])
const hasNoItems = useFacetMap((facet) => Boolean(!facet.items.length), [], [itemsFacet])

return (
  <>
    <Mount when={hasItems}>
      <p>Has items</p>
    </Mount>
    <Mount when={hasNoItems}>
      <p>Has no items</p>
    </Mount>
  </>
)

My suggestion is to make the Mount component accept an optional condition prop to be able to flip booleans from the same facet:

const itemsFacet = useFacetMap((facet) => facet.items, [], [useRemoteFacet(someFacetSelector)])
const hasItems = useFacetMap((facet) => Boolean(facet.items.length), [], [itemsFacet])

return (
  <>
    <Mount when={hasItems}>
      <p>Has items</p>
    </Mount>
    <Mount when={hasItems} condition={false}>
      <p>Has no items</p>
    </Mount>
  </>
)

@creativecreature creativecreature added the enhancement New feature or request label Jan 5, 2022
@xaviervia xaviervia enabled auto-merge (squash) January 19, 2022 10:29
@xaviervia xaviervia merged commit 0bf7d71 into main Jan 19, 2022
@xaviervia xaviervia deleted the mount-condition branch January 19, 2022 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants