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

[Feature Request]: Allow hooks to be passed in component or similar property - React/Typescript #23216

Open
kdavidenkovmware opened this issue Jun 26, 2023 · 2 comments · May be fixed by #28910

Comments

@kdavidenkovmware
Copy link

kdavidenkovmware commented Jun 26, 2023

Is your feature request related to a problem? Please describe

Our organization has several hooks documented with storybook, using a component in the story to render them, but on two of them we need to bring in the associated jsdoc commentary which can only be achieved at the moment by passing the hook as the component like so:

const meta: Meta<typeof useReactHook> = {
  title: 'Hooks/useReactHook',
  component: useReactHook,
};
export default meta;

With the goal of a result like:
Screen Shot 2023-06-26 at 4 51 08 PM

however, written in this results in a typescript error, as hooks themselves are not rendered components.

Describe the solution you'd like

In a React/Typescript environment, we would like to be able to pass hooks either as the component or with their own key.

Describe alternatives you've considered

The main workaround we've found is the following, but it requires an as type assertion:

const meta: Meta<typeof useReactHook> = {
  title: 'Hooks/useReactHook',
  component: useReactHook as ComponentType<unknown>,
// or component: useReactHook  as unknown as ComponentType<unknown>,
};
export default meta;

Are you able to assist to bring the feature to reality?

no

Additional context

No response

@Danielmart02
Copy link

I am working on this.

@Danielmart02
Copy link

Hi @kdavidenkovmware ,

I’ve been working on a workaround for the issue regarding passing React hooks as components in Storybook. Here’s a solution that I’ve found effective:

Solution Overview

The workaround involves creating a custom Storybook configuration to handle TypeScript types and JSDoc documentation for React hooks. This approach allows you to use hooks in Storybook while ensuring they are properly documented and tested. The solution includes updating your Storybook setup and writing the hook with appropriate JSDoc comments.

Detailed Steps

Here’s an example of a React hook with JSDoc comments:

import { useState } from 'react';

/**
 * A custom hook for managing text state.
 * @returns An array containing the current text and a function to update it.
 * @example
 * const [text, setText] = useExampleHook('initial text');
 * setText('new text');
 */
export function useExampleHook(initialText: string): [string, (text: string) => void] {
  const [text, setText] = useState(initialText);

  return [text, setText];
}

Add a Story for the Hook

Create a story for the custom hook in Storybook:


import type { Meta, StoryObj } from '@storybook/react';
import { useExampleHook } from './useExampleHook';

const meta: Meta<typeof useExampleHook> = {
  title: 'Example/UseExampleHook',
  component: useExampleHook,
  tags: ['autodocs'],
  parameters: {
    layout: 'fullscreen',
  },
};

export default meta;

type Story = StoryObj<typeof meta>;

export const Default: Story = {
  render: () => {
    const [text, setText] = useExampleHook('Hello, world!');
    return (
      <div>
        <p>Current text: {text}</p>
        <button onClick={() => setText('New text')}>Change Text</button>
      </div>
    );
  },
};

Additional Notes

Testing: After updating the configuration and creating the story, I tested it by running Storybook and verifying that the custom hook’s documentation was displayed correctly.

Feedback: I’d appreciate feedback on this workaround and any additional suggestions for improvements.

Feel free to test this workaround and let me know if you encounter any issues or have further questions. I’m happy to assist with any clarifications needed.

Best,
Daniel Mart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: No status
Development

Successfully merging a pull request may close this issue.

2 participants