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

Redux connect support #80

Closed
maximelebastard opened this issue May 25, 2016 · 5 comments
Closed

Redux connect support #80

maximelebastard opened this issue May 25, 2016 · 5 comments

Comments

@maximelebastard
Copy link

Hi,

I have a React/Redux app that I'd like to document. With redux, I use the connect() function.

But when I use it, react-docgen cannot find a valid component in my file.

This works:

const Contributors = React.createClass({...});
export default Contributors;

This does not:

const Contributors = React.createClass({...});
const mapStateToProps = (state) => {
  return { contributors: state.contributors }
}
export default connect(mapStateToProps)(Contributors);

I found a workaround by appending this to the end of file, but I don't know if it will not break something later:

module.exports = Contributors;
@rtsao
Copy link
Contributor

rtsao commented May 25, 2016

I've run into this issue as well. I've added support for single unary higher-order-components in my personal fork. This is the relevant commit here: rtsao@9ca8127

You can turn your code to use a single unary higher-order component like so:

const Contributors = React.createClass({...});
const mapStateToProps = (state) => {
  return { contributors: state.contributors }
}

const hoc = (Component) => connect(mapStateToProps)(Component);

export default hoc(Contributors);

The above code will work with my fork.

If multiple higher order components are needed, you can use recompose's compose helper.

I think only supporting a single unary higher-order component seems like a reasonable compromise, since basically you can convert all forms of higher-order component composition into that form. To support multiple (or an arbitrary number of) call expressions or arguments it seems like one would need to go down a rabbit hole.

I'd be happy to create a PR if @fkling thinks my approach is reasonable. I haven't yet done so already because I worry I might have broken some existing code structure conventions (by requiring '../utils/match' and 'recast' directly within a resolver), but that's probably a straightforward refactor.

@fkling
Copy link
Member

fkling commented Jun 1, 2016

@rtsao:

I've added support for single unary higher-order-components in my personal fork

That seems very reasonable to me, and I think it would even make sense to expand this to non-unary functions. The other arguments could be included in the output as well, as far as possible. Happy to review that PR :)

@maximelebastard
As an intermediate solution, if you can use the findAllComponentDefinitions resolver, which looks for all component definitions, not just the exported ones.

@tizmagik
Copy link

tizmagik commented Jun 3, 2016

@rtsao that looks very useful! Was a PR ever submitted for this?

To support multiple (or an arbitrary number of) call expressions or arguments it seems like one would need to go down a rabbit hole.

Maybe naive, but would it be possible to recursively resolve the "right" hand side expression until you reach a React class or undefined?

@rtsao
Copy link
Contributor

rtsao commented Jun 3, 2016

Yeah, actually I think this should be very doable.

export default hoc1(Component);

export default hoc1(arg1a, arg1b)(Component);

export default hoc2(arg2b, arg2b)(
  hoc1(arg1a, arg2a)(Component)
);

export default hoc3(arg3a, arg3b)(
  hoc2(arg2b, arg2b)(
    hoc1(arg1a, arg2a)(Component)
  )
);

// etc...

When I get a chance I'll give it a go and submit a PR

@rtsao
Copy link
Contributor

rtsao commented Sep 14, 2016

I've implemented this here: #124

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants