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

Added @:wrap support #102

Merged
merged 1 commit into from
May 1, 2018
Merged

Added @:wrap support #102

merged 1 commit into from
May 1, 2018

Conversation

kLabz
Copy link
Contributor

@kLabz kLabz commented Apr 29, 2018

Use the power of @:jsxStatic to wrap a component with multiple wrappers/HOCs: redux's connect, i18n with intl, react-router's withRouter, etc. with a simple meta.

To wrap a component with a single wrapper:

@:wrap(ReactRouter.withRouter)
class MyComponentWithRouter extends ReactComponentOfProps<MyProps>
{
	// ...
}

// And use it with
jsx('<$MyComponentWithRouter />')

You can also combine wrappers with multiple metas, @:wrap instructions' order will be respected (the first one will be the entry point, and the last one will provide the final props to the component):

@:wrap(ReactRouter.withRouter)
@:wrap(ReactIntl.injectIntl)
@:wrap(ReactRedux.connect(MyComponentWithWrappers.mapStateToProps))
class MyComponentWithWrappers extends ReactComponentOfProps<MyProps>
{
	// ...
}

// And use it with
jsx('<$MyComponentWithWrappers />')

Will generate the equivalent of MyComponentWithWrappers = ReactRouter.withRouter(ReactIntl.injectIntl(MyComponent))


static public function buildComponent(inClass:ClassType, fields:Array<Field>):Array<Field>
{
if (inClass.meta.has(WRAPPED_META)) return fields;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really cool, but is it possible for multiple builds to happen?

Copy link
Contributor Author

@kLabz kLabz Apr 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With compilation server, if the component (or its meta) is modified it looses all macro-generated meta and its build macros are re-run.

Edit: as for the usefulness of this check, the macro can be called multiple times if there are onAfterGenerate (I think it is this one) handlers triggering another build.

@zabojad
Copy link

zabojad commented Apr 30, 2018

HOC... Now we are talking...

@kLabz
Copy link
Contributor Author

kLabz commented May 1, 2018

@zabojad: btw, atm you can use HOCs like this:

@:jsxStatic('connectedWithIntl')
class MyComponent extends ReactComponentOfProps<Props> {
    public static var connectedWithIntl = ReactRedux.connect(mapState)(injectIntl(MyComponent));

    static function mapState(state:AppState, ownProps:OwnProps) {
        // ...
    }

    // ...
}

And call your component with

jsx('<$MyComponent />')

@elsassph elsassph merged commit d48d520 into massive-oss:master May 1, 2018
@elsassph
Copy link
Contributor

elsassph commented May 1, 2018

Thanks for that - we need to document it a bit now.

Also worth noting that it won't work in dynamic scenarios as @:jsxStatic only is effective when the type can be resolved when processing the JSX.

@zabojad
Copy link

zabojad commented May 1, 2018

@kLabz thank you, it trully is awesome! It also needs to be documented clearly in the haxe-react doc...

@kLabz
Copy link
Contributor Author

kLabz commented Sep 7, 2018

If anyone's interested, I have improved and documented @:wrap in my temporary fork (all WIP docs here).

I would add the docs here (like @:jsxStatic) but the things I am documenting have actually changed quite a bit in my fork (mostly because of the much stricter typing) so it'd be too much work for the time I can afford to spend on it.

I am working on making this fork "mergeable" in a not-so-far future though, since I definitely want these contributions to come here at the end. And I will keep creating PRs for features/documentations that are compatible with current haxe-react.

@kLabz kLabz deleted the feature/wrap branch January 17, 2019 08:53
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 this pull request may close these issues.

3 participants