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 Proptypes for wrapped and wrapper components #8

Closed
saulshanabrook opened this issue Sep 3, 2015 · 11 comments
Closed

Add Proptypes for wrapped and wrapper components #8

saulshanabrook opened this issue Sep 3, 2015 · 11 comments

Comments

@saulshanabrook
Copy link

It would be nice to be able to add propTypes to both the inner and outer components returned by the decorator.

@christianalfoni
Copy link
Collaborator

Hm... do not think it is common to allow that, any particular reason? :-)

@saulshanabrook
Copy link
Author

For example, I have a component called Systems that other components should pass a systemsPath prop when creating. They are passing it to the cerebral wrapper, so Systems Container. Then the actual Systems component gets a couple of props passed in that cerebral feeds it, but doesn't see the systemsPath prop.

@Cerebral(props => ({
  systems: props.systemsPath,
  newSystem: getNewSystemPath(props.systemsPath)
}))
class Systems extends Component {
  [...]
}

// here i want to define the propTypes on the wrapped system
Systems.propTypes = {
  systemsPath: React.PropTypes.array.isRequired
}
// but i would also like to set the propTypes on the inner System to this:
// {
//  systems: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
//  newSystem: React.PropTypes.object
//}

export default Systems

I want to make sure that:

  1. the component is being called by other components with the right prop types.
  2. cerebral is getting the right types from the state to pass into the

Does that makes sense?

@christianalfoni
Copy link
Collaborator

Hm hm, first thanks for explaining it so thoroughly :-)

I do not quite understand why it is not good enough to do:

Systems.propTypes = {
  systems: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
  newSystem: React.PropTypes.object
}

Since it is just passing the props straight through I can not quite see it is relevant to "double check"?

@saulshanabrook
Copy link
Author

But then how do I set the prop types for the wrapped component? Like where do I set these prop types

{
  systemsPath: React.PropTypes.array.isRequired
}

@christianalfoni
Copy link
Collaborator

Ah, now I understand why you mean, sorry.

Are these propTypes checked at runtime? If so Cerebral-React should throw an error when an invalid path is used. So this would be handled automatically. Would that make sense? It feels a bit overkill to make an API change to handle this, as you can only use arrays there.

@saulshanabrook
Copy link
Author

Yeah propTypes are checked at runtime. Hm, what do you mean by "invalid path"? The props don't have to be path's, that is just an example, they could be any type/value.

For example...

@Cerebral(props => ({
  systems: ['systems', props.systemID],
}))
class System extends Component {
  [...]
}

// here i want to define the propTypes on the component that I call create elsewhere in my code
System.propTypes = {
  systemID: React.PropTypes.number.isRequired
}
// but i would also like to set the propTypes on the inner component, that cerebral initializes, to this:
// {
//  systems: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
//}

export default System

Does that makes sense as an example?

As a solution, maybe we could pass the propTypes in to the decorator?

@Cerebral(props => ({
  systems: ['systems', props.systemID],
}), {
  systems: React.PropTypes.arrayOf(React.PropTypes.object).isRequired,
})
class System extends Component {
  [...]
}

System.propTypes = {
  systemID: React.PropTypes.number.isRequired
}

export default System

@tnrich
Copy link

tnrich commented Oct 4, 2015

@saulshanabrook, I think that your proposal makes sense. Would it be hard to implement that strategy @christianalfoni ?

@tnrich
Copy link

tnrich commented Oct 13, 2015

@saulshanabrook This seems like it would be a more general issue with using decorators with React. Maybe others in the community have a solution for it?

@tnrich
Copy link

tnrich commented Oct 14, 2015

hey @saulshanabrook and @christianalfoni , tested it out and it looks like using:
https://github.com/popkirby/react-props-decorators
will work just fine!

An example is:

@Cerebral({
    sequenceLength: ['sequenceLength'],
    bpsPerRow: ['bpsPerRow'],
})
@propTypes({
    sequenceLength: PropTypes.number.isRequired,
    bpsPerRow: PropTypes.number.isRequired,
})
class SequenceEditor extends React.Component {
...

This seems like a good general solution that doesn't require cerebral to add anything specific. It might be nice for others to know about it though if you wanted to put something in the docs about it.

Also I forked it and updated the dependencies and made a pull request so hopefully that'll get updated soon.

@saulshanabrook
Copy link
Author

@tnrich Wow that looks great! I think there should be a section in the docs about how to do this with react. @christianalfoni I would be happy to have a go at it, if you think that makes sense.

@christianalfoni
Copy link
Collaborator

@saulshanabrook Yeah, please add it to the docs, that would be great :-)

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

3 participants