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

Handling anything (not just component or function) #6

Open
Offirmo opened this issue Jul 18, 2018 · 2 comments
Open

Handling anything (not just component or function) #6

Offirmo opened this issue Jul 18, 2018 · 2 comments

Comments

@Offirmo
Copy link

Offirmo commented Jul 18, 2018

I'm not 100% sure, but when using the "children" mode:

<foo>
   Hello
</foo>

with a render like that:

const content = this.props.children || this.props.render
return renderProps(content, this.props)

It fails pitifully with Cannot read property 'render' of undefined.

My guess is that's coming from the component detection in render-props.

Could we make it work?

@Offirmo
Copy link
Author

Offirmo commented Jul 18, 2018

What I ended up implementing:

const { children, render, ...props } = this.props
const ComponentOrFunctionOrAny = children || render || DEFAULT_CONTENT

// is it a react component?
if (ComponentOrFunctionOrAny.render || (ComponentOrFunctionOrAny.prototype && ComponentOrFunctionOrAny.prototype.render))
  return <ComponentOrFunction {...props} />

// is it a render function?
if (typeof ComponentOrFunctionOrAny === 'function')
  return ComponentOrFunctionOrAny({
    ...(ComponentOrFunctionOrAny.defaultProps || {}),
    ...props,
})

// must be null or a string
return ComponentOrFunctionOrAny

Maybe naive but it works in my use case.

[edit] removed a wrong Component detection

@peteruithoven
Copy link

Another way is adding a check before using this library:

if (React.isValidElement(children)) return children;
else return renderProps(component || children || render, props);

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

2 participants