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 support for hooks #69

Open
Tsugami opened this issue Dec 10, 2021 · 0 comments
Open

add support for hooks #69

Tsugami opened this issue Dec 10, 2021 · 0 comments

Comments

@Tsugami
Copy link

Tsugami commented Dec 10, 2021

I would like to create separate components with CollapseHeader and CollapsedBody, without the need to do dirty props, I think hooks improve the experience and would make the code cleaner.

example without hook:

// parent component should have a useState to dirty props
function Header({ title, isExpanded }: Props) {
  return (
    <CollapseHeader>
      <TitleHeader>
        <Title>{title}</Title>
        <Feather name={isExpanded ? 'chevron-down' : 'chevron-up'} size={15} color='#fff' />
      </TitleHeader>
    </CollapseHeader>
  );
}

function Example() {
  const [isExpanded, setExpanded] = React.useState(true);

  return (
    <Collapse isExpanded={isExpanded} onToggle={() => setExpanded(!isExpanded)}>
      <Header title="Hello" isExpanded={isExpanded} />
      { /* ... */ }
    </Collapse>
  );
}

example with hook:

function Header({ title }: Props) {
  const { isExpanded } = useCollapse();

  return (
    <CollapseHeader>
      <TitleHeader>
        <Title>{title}</Title>
        <Feather name={isExpanded ? 'chevron-down' : 'chevron-up'} size={15} color='#fff' />
      </TitleHeader>
    </CollapseHeader>
  );
}

function Example() {
  return (
    <Collapse>
      <Header title="Hello" />
      { /* ... */ }
    </Collapse>
  );
}
@Tsugami Tsugami changed the title add support a hooks add support for hooks Dec 10, 2021
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