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

defaultProps treated differently than in preact #58

Closed
ftes opened this issue Jun 13, 2018 · 3 comments · Fixed by #60
Closed

defaultProps treated differently than in preact #58

ftes opened this issue Jun 13, 2018 · 3 comments · Fixed by #60

Comments

@ftes
Copy link
Contributor

ftes commented Jun 13, 2018

This library behaves differently than preact when dealing with undefined prop values.

Expected behaviour:
preact-render-to-string should behave like preact:

  1. A default value is set for a prop (MyComponent.defaultProps.someProp = 'X')
  2. undefined is passed as prop value (<MyComponent someProp={undefined}/>)
  3. I expect to get the default value within the instantiated component (console.log(this.props.someProp) >> 'X')

Actual behaviour:
If undefined is passed as prop value, then this.props.someProp resolves to undefined.

Here you can see how the implementation in preact and preact-render-to-string differ:

for (let i in defaultProps) {
  if (props[i]===undefined) {
    props[i] = defaultProps[i];
  }
}
let defaultProps = vnode.nodeName.defaultProps,
  props = assign({}, defaultProps || vnode.attributes);
if (defaultProps) assign(props, vnode.attributes);
@developit
Copy link
Member

Hi @ftes! You're right, we're not handling undefined/null correctly in render-to-string.
Would you be interested in submitting a PR? :)

ftes pushed a commit to ftes/preact-render-to-string that referenced this issue Aug 7, 2018
@ftes
Copy link
Contributor Author

ftes commented Aug 7, 2018

Sure thing, PR created @developit

developit pushed a commit that referenced this issue Aug 16, 2018
@developit
Copy link
Member

Thanks, it's out in 3.8.1!

marvinhagemeister pushed a commit to preactjs/preact that referenced this issue Mar 15, 2022
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

Successfully merging a pull request may close this issue.

2 participants