npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

haxe-react-router

v0.0.6

Published

Haxe externs for react-router 4

Downloads

18

Readme

Haxe react-router 4

A Haxe library offering externs for version 4 of the react-router library. For version 3, see haxe-react-router by elsassph.

Installation

Using haxelib:

haxelib install react-router-4

Usage

An extensive documentation is available here for the javascript react-router library and is mostly applicable for this library too.

React Router API

ReactRouter.matchPath(pathname:String, props:RouteMatchProps):Null<RouterMatch>

This lets you use the same matching code that <Route> uses except outside of the normal render cycle, like gathering up data dependencies before rendering on the server.

See https://reacttraining.com/react-router/web/api/matchPath

ReactRouter.withRouter(component:CreateElementType):CreateElementType

You can get access to the history object’s properties and the closest <Route>'s match via the withRouter higher-order component. withRouter will re-render its component every time the route changes with the same props as <Route> render props: { match, location, history }.

See https://reacttraining.com/react-router/web/api/withRouter

With haxe-react version 1.4.1+ (not released at the moment, but this is available by using the git version), you can use @:wrap to easily wrap your component with a withRouter call:

@:wrap(react.router.ReactRouter.withRouter)
class MyComponent extends ReactComponentOfProps<Props> {
}

React Router components

React Router components are available in the react.router package.

Each component has its typed props with comments and a link to the javascript react-router documentation, which applies to this library.

List of available components:

Code splitting with haxe-loader

When using the webpack haxe-loader library, you can use react.router.bundle.Bundle.load(...) to split your application into bundles directly in your routes:

import react.ReactMacro.jsx;
import react.router.BrowserRouter;
import react.router.Route;
import react.router.Switch;
import react.router.bundle.Bundle;

class MainRouter extends ReactComponent {
    override public function render() {
        return jsx('
            <$BrowserRouter>
                <$Switch>
                    <!-- Using default loader component (`<div className="loader" />`) -->
                    <!-- and default error component (`<div className="error" />`) -->
                    <$Route
                        path="/bundle1"
                        component=${Bundle.load(first.FirstBundle)}
                    />

                    <!-- Using custom loader and/or error component -->
                    <!-- The error component will get an `error` prop with the load error as `Dynamic` -->
                    <$Route
                        path="/bundle2"
                        component=${Bundle.load(second.SecondBundle, CustomLoader, CustomError)}
                    />
                </$Switch>
            </$BrowserRouter>
        ');
    }
}

Usage with @:jsxStatic components

See the @:jsxStatic original PR on haxe-react if you don't know about @:jsxStatic components. Documentation will be added on haxe-react later.

You cannot directly load @:jsxStatic components with current (1.4.0) haxe-react version. There is a merged PR to make it work. This is not released at the moment, but until 1.4.1+ you can use git version of haxe-react.

If you really need to, another workaround is to use a wrapper around your @:jsxStatic component:


// In your router
jsx('
    <$Route path="/bundle3" component=${Bundle.load(MyWrapper)} />
');

// In your bundle's module
class MyWrapper extends ReactComponent {
    override public function render() {
        return jsx('
            <$MyComponent {...props} />
        ');
    }
}

@:jsxStatic('render')
class MyComponent {
    public static function render(props:Dynamic) {
        return jsx('<div>My component</div>');
    }
}

Bundle initialization code

You may want to execute some code when your bundle is first loaded.

When your component is loaded via Bundle.load(...) (or ILazyLoad, see below), you can define initialization code to be executed at first load by creating an onLoad static method on your component:

import react.ReactComponent;
import react.router.Route.RouteRenderProps;

@:expose('default')
class MyBundle extends ReactComponentOfProps<RouteRenderProps> {
    // If you want to execute code when this bundle is _first_ loaded:
    public static function onLoad() {
        // ...
    }

    // ...
}

ILazyLoad macro (experimental)

If you use custom loaders and/or custom error components, Bundle.load() calls can become quite verbose.

There is an experimental API based on macros to define your async routes like this:

import react.ReactMacro.jsx;
import react.router.BrowserRouter;
import react.router.Route;
import react.router.Switch;
import react.router.bundle.ILazyLoad;

// Magic interface with build macro
// To easily create component bundles
class LazyLoad implements ILazyLoad<[
    my.first.app.FirstApp,
    my.second.app.SecondApp,
    my.third.app.ThirdApp
]> {
    // A build macro will create 3 static functions:
    // - FirstApp, which will render my.first.app.FirstApp
    // - SecondApp, which will render my.second.app.SecondApp
    // - ThirdApp, which will render my.third.app.ThirdApp

    // Warning: due to this notation, you cannot lazy load two components with
    // the same name from two different packages.


    // You can also define a loader component, if you want
    // If you don't, a <div className="loader"></div> will be rendered instead.
    static var loading = my.components.Loader;

    // Same thing for the error component.
    // Both loaderComponent and errorComponent can be either a static var or a
    // static function.
    // The error component will receive an `error` prop with the load error.
    // If you don't provide any, a <div className="error"></div> will be
    // rendered if the require fails.
    static function error() {
        return jsx('<div className="error">Error!</div>');
    }
}

You can then use them in your react-router routes:

class MyRouter extends ReactComponent {
    override public function render() {
        return jsx('
            <$BrowserRouter>
                <$Switch>
                    <$Route path="/first" component=${LazyLoad.FirstApp} />
                    <$Route path="/second" component=${LazyLoad.SecondApp} />
                    <$Route path="/third" component=${LazyLoad.ThirdApp} />
                </$Switch>
            </$BrowserRouter>
        ');
    }
}