Comments (10)
This all stems from my fondness of exporting everything (for unit-tests and storybooks) but making a default export that is the complete redux-connected component, as I use it my app. Most of my code looks like this, and I'd love to be able to get the doctext out:
import React from 'react'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
/** A dumb component that says "hello" */
export const Demo = ({name}) => (<div>Hello {name}</div>)
Demo.propTypes = {
/** the name of the person to say "hello" to */
name: PropTypes.string
}
Demo.defaultProps = {
name: 'World'
}
export const mapStateToProps = state => ({
name: state.name
})
export default connect(mapStateToProps)(Demo)
This makes it so I can unit-test my state-mappings, the Component (without redux) and write storybook stories for just the component, but it's also super-simple to use in my redux-enabled app.
from babel-plugin-react-docgen.
@konsumer @danielduan i made a pr which allows you to use one of the builtin resolvers of react-docgen by passing options to the babel plugin.
I'm not sure how to write the tests for it however :(
from babel-plugin-react-docgen.
It looks like we might not support stateless functional components like function Component() {}
. A workaround would be to write it as Component() => {}
. You are welcome to submit a PR to add this though.
We haven't tested it with Docgen 3, but that's probably not the issue here.
from babel-plugin-react-docgen.
@danielduan Thanks for the feedback. Per your suggestion, I tried a couple different approaches and found the following.
These work:
const Button = () => { /*omitted*/ }
export default Button;
class Button extends Component { /*omitted*/ }
export default Button;
These don't work:
function Button() { /*omitted*/ }
export default Button;
export default class Button extends Component { /*omitted*/ }
Also, thanks for opening the door for a PR. I would do it but I don't have the time at this very moment. I wouldn't totally mind opening a PR to document more explicitly what syntax is valid for this plugin to kick in. I see there is an example on the README that conflicts with what I've found, I wouldn't mind updating that example.
from babel-plugin-react-docgen.
I am having a similar issue (see here and here for a demo.) I can make it work if I only export 1 interface (stateless function or not) but I like to export default + individual components.
Maybe a simple detect-react-components strategy would help:
- If only default or 1 exported function, that's the react component
- If there is more than one exported function (default and/or other exports) try to annotate them all, I don't really mind if other non-react-component functions get annotated, maybe hide behind a flag to not be so greedy about matching functions
If this makes sense, I'll work on a PR.
from babel-plugin-react-docgen.
I think there's a few bugs here:
- inlined export doesn't get detected
export default class Button
- named exports don't get docgen info when there's a default export
function Button()
doesn't get recognized as stateless functional components
Your approach sounds good and will fix 2, but you might need to address 1 as well.
from babel-plugin-react-docgen.
Yeh, I see your point. Maybe 1 could be resolved with "extends React.Component?" check. I feel like a straight check for "of the things exported, if it's a function or extends React.Component, or is createReactClass" would, if not overly-greedy, hit all the targets. As I said, I don't really mind if too much is annotated, as it can just be ignored. Does this seem like a sensible approach?
from babel-plugin-react-docgen.
I made an issue (and patch) here that resolves this for my use-case. I suspect that I am probably missing some key detail, but if it gets in, it should resolve the issue.
from babel-plugin-react-docgen.
In CLI it seems to work fine with a different built-in resolver, but I don't see any way to add that config to babel-plugin-react-docgen, as it's used in storybook's webpack config.
react-docgen --pretty --resolver findAllComponentDefinitions src/stories/Demo.js
:
[
{
"description": "A dumb component that says \"hello\"",
"methods": [],
"props": {
"name": {
"type": {
"name": "string"
},
"required": false,
"description": "the name of the person to say \"hello\" to",
"defaultValue": {
"value": "'World'",
"computed": false
}
}
}
}
]
If the default were findAllComponentDefinitions
, or we were able to override the resolver with a config var, it would make it work for my use-case.
from babel-plugin-react-docgen.
We could expose it as an option for the babel plugin. I'm gonna guess there's probably a way to pass something to react-docgen to turn on findAllComponentDefinitions too.
Sorry I'm not too responsive this past week and I'm gonna be away for the next one as well. If you find a solution, please do update and open a PR.
from babel-plugin-react-docgen.
Related Issues (20)
- [Bug] docgen works with start-storybook but not build-storybook
- [Question] babel-plugin-react-docgen-typescript alternatives ? HOT 1
- Are we open to changing the test file names? HOT 1
- Does not work in my case if I use the babel configuration in webpack HOT 2
- "Property value expected type of" error for an unnamed function HOT 3
- Update to babel 7? HOT 1
- If using reacts injectIntl to wrap component __docgenInfo is undefined HOT 2
- Error: Cannot find module 'recast' HOT 6
- [react-docgen]Smartknobs is not showing up
- Does not work with `class` Components HOT 12
- Not working with `forwardRef` wrapped components HOT 7
- How to docgen for an npm package? HOT 1
- Upgrade to [email protected] HOT 11
- The issue with forwardRef Component HOT 2
- React.createRef could breaking generating doc
- Cannot define custom resolver HOT 1
- Cannot pass Babel parser options to react-docgen
- Update `actualNameHandler` to remove FB patent license
- "Component".__docgenInfo no available on 4.2.0 HOT 5
- Dependency Version Update (react-docgen) HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from babel-plugin-react-docgen.