AlecAivazis / react-event-wrappers

A lightweight wrapper for React components which helps bind components to various events
0 stars 0 forks source link

Wrapping stateless functional components #4

Open montemishkin opened 9 years ago

montemishkin commented 9 years ago

We should support wrapping stateless functional components (see here).

This shouldn't be too hard. Our decorator should check if the thing its wrapping has extended React.Component. If it has, do as we already do. If it hasn't (and is a function) then we make a class that extends React.Component that will essentially look something like

class WrappedComponent extends React.Component {
    render() {
        return wrapped_stateless_functional_component(this.props)
    }
}

and then use this WrappedComponent like we already do.

montemishkin commented 9 years ago

I'd imagine that (unless I'm missing something here) lots of people are going to be implementing this same functionality, which detects if a component extends React.Component or is a stateless functional component, and then converts to a class extending React.Component if it is stateless. So maybe that part of the solution should be separated into a standalone npm package.

montemishkin commented 9 years ago

Looks like react already has this feature for detecting if a component is stateless functional or not:

if (Component.isReactClass) {
    // then it is a class which extends React.Component
}

See here (and the commit which closed that issue) for more info.

montemishkin commented 9 years ago

Also, found this for turning stateless functional components into react classes.