Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable prop-types in production for a React Storybook for the Docs addon

By default prop-types do not run in production for a react app. I realize this is a good thing to improve performance. However, we have a Storybook that we have built and are deploying it to a static site. Storybook has an addon called Docs that detects the prop-types for components and creates a table of the prop-types for easy to read documentation.

When running the storybook locally, everything works perfectly. The prop-types are detected and this table is generated.

SpinningLoader.propTypes = {
  color: PropTypes.string,
  size: PropTypes.oneOfType([PropTypes.string, PropTypes.number])
};

enter image description here

However, since prop-types are disabled in production by default. They cannot be detected when the storybook is deployed to a static site.

enter image description here

Is there a way to enable prop-types in production? Or some other workaround?

like image 264
Kevin Vandy Avatar asked Apr 29 '20 15:04

Kevin Vandy


People also ask

How do you assign a prop to type in React?

To run typechecking on the props for a component, you can assign the special propTypes property: import PropTypes from 'prop-types'; class Greeting extends React. Component { render() { return ( <h1>Hello, {this.props.name}</h1> ); } } Greeting. propTypes = { name: PropTypes.

What version of React does storybook use?

Storybook 6.1 also supports React 17 as well as fast refresh and strict mode. React 17 is a non-breaking major release of React that sets the stage for the next round of innovations. It includes a JSX transform that allows you to write JSX without importing React at the top.


1 Answers

It's a little difficult to know without seeing more of your setup. If you're building it with the default storybook commands without and additional configuration it should "just work"...as far as I can tell.

As mentioned in a comment, Storybook has a specific build command you can add to your package.json to export it as a static app:

"scripts": {
  "build-storybook": "build-storybook -c .storybook -o .out"
}

If you're using that command and it's still not working, are you using any custom webpack/build workflow, and can you post those as well?

I've built a minimal repository for reference, which may be helpful in comparing your setup. Besides the packages in package.json it's really only 3 files; Storybook config, a React component, and a Component Story:

.storybook/main.js

module.exports = {
  stories: ['../src/**/*.stories.[tj]s'],
  addons: ['@storybook/addon-docs'],
};

src/components/message/message.js

import React from 'react'
import PropTypes from 'prop-types'

const Message = function Message({ text, color }) {
  return (<div style={{ color }}>{text}</div>)
}

Message.propTypes = {
  text: PropTypes.string.isRequired,
  color: PropTypes.string.isRequired,
}

export default Message

src/components/message/message.stories.js

import React from 'react'
import Message from './message'

export default { title: 'Message', component: Message }

export const withText = () => <Message text="Hello World" color="green" />

If I run the build-storybook command, cd .out, and then npx live-server, I see the static-built storybook site, with my Message component, and the 'Docs' tab that includes the prop-types:

enter image description here

Full repository for reference

https://github.com/BenjaminWFox/react-storybook

like image 140
Ben Avatar answered Oct 22 '22 18:10

Ben