Skip to content

How do I access state from deeply nested components? #2206

Answered by diegohaz
crisshaker asked this question in Questions
Discussion options

You must be logged in to vote

useRadioGroup is just a custom hook that returns HTML props so you can create your own RadioGroup component. It doesn't give you access to the radio state.

The obvious ways are to prop-drill the value or write my own Provider.

That's the way. You can also try constate if you're doing that very often:

import { useRadioState, RadioGroup, Radio } from "ariakit";
import constate from "constate";

const [RadioProvider, useRadioContext] = constate(useRadioState);

function Wrapper() {
  // can't useRadioContext here
  return (
    <RadioProvider defaultValue="orange">
      <MyRadioGroup />
    </RadioProvider>
  );
}

function MyRadioGroup() {
  const state = useRadioContext();
  return (
    <

Replies: 1 comment 1 reply

Comment options

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

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