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

Remove mixins #6

Open
slorber opened this issue Mar 27, 2015 · 0 comments
Open

Remove mixins #6

slorber opened this issue Mar 27, 2015 · 0 comments

Comments

@slorber
Copy link
Contributor

slorber commented Mar 27, 2015

React libs tends to use mixins but it would be better to not use them anymore.

See
https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750

Instead of using mixins for shouldComponentUpdate we can just add the shouldComponentUpdate method if it is not defined by the user in this place:

function addMixins(config) {
    config.mixins = config.mixins || [];
    config.mixins.push(WithPureRenderMixin);
    config.mixins.push(WithCursorLinkingMixin);
    config.mixins.push(WithTransactMixin);
    config.mixins.push(WithCommandPublisherMixin);
    config.mixins.push(WithEventPublisherMixin);
    config.mixins.push(WithEventListenerMixin);
}

function createPureClass(name,component) {
    Preconditions.checkHasValue(name,"The name attribute is mandatory: this helps to debug compoennts!")
    Preconditions.checkHasValue(component,"The config attribute is mandatory!")
    Preconditions.checkCondition(!component.initialState,"Pure components should not have any local state, and thus no initialState attribute");
    Preconditions.checkCondition(!component.shouldComponentUpdate,"shouldComponentUpdate is already implemented for you");
    Preconditions.checkCondition(component.render,"render() must be implemented");
    Preconditions.checkCondition(component.propTypes,"propTypes must be provided: this is the component interface!");

    // Unfortunately, the displayName can't be infered from the variable name during JSX compilation :(
    // See http://facebook.github.io/react/docs/component-specs.html#displayname
    component.displayName = name;
    // Because React's displayName is not easy to obtain from a mixin (???)
    component.getDisplayName = function() { return name };


    addMixins(component);

    return React.createClass(component);
}
exports.createPureClass = createPureClass;

Also we want to abstract the use of React's context feature from the user as it is still undocumented, so we can add a method to get access to AtomReact context methods, probably by merging contextTypes required by AtomReact to contextTypes defined by the user.

Also it would be nice to work around the problem of JSX's displayName inference.
The fact that we use a custom class factory means that JSX can't infer the displayName anymore and to have a displayName it is required to use something like:

var Footer = AtomReact.createPureClass("Footer",{ 
   ...
});

Can we make the inference work wiith a custom class factory?

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

1 participant