Skip to content

Can't access ListBoxContext in child of ListBox #6841

Answered by devongovett
jaslong asked this question in Q&A
Discussion options

You must be logged in to vote

Linking to my answer from a previous discussion on this: #6596 (comment)

What are you needing off of the state in your item component? One thing that does work, is to split the component in two, like this:

function MyListBoxItem(props) {
  return (
    <RACListBoxItem {...props}>
      <MyListBoxItemInner {...props} />
    <RACListBoxItem>
  );
}

function MyListBoxItemInner(props) {
  let ctx = useContext(ListBoxContext);
  // do whatever
}

This works because the children of MyListBoxItem are rendered lazily, only after the initial collection is constructed during the first render. At that point the state is readable.

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jaslong
Comment options

Answer selected by jaslong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants