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

Programmatical API #137

Closed
azer opened this issue Feb 2, 2017 · 7 comments
Closed

Programmatical API #137

azer opened this issue Feb 2, 2017 · 7 comments

Comments

@azer
Copy link

azer commented Feb 2, 2017

I'm working on a Go framework that needs to combine routing in one JSON document that can be read by both JS and Go. To accomplish this, I can't hard-code the routes, I need an API.

Is there an example of programmatical usage of preact-router? Documentation seem to not cover such a case.

@developit
Copy link
Member

Not quite sure I understand - if you mean programmatically triggering a route you can use route():

import { route } from 'preact-router';
route('/foo');

@developit
Copy link
Member

Just re-read - for a routing table you would just use a function to convert your routes to children:

import routes from './routes.json';
import pages from './pages'; // map of components

<Router>
  { routes.map( r => (
    <Route {...r} component={pages[r.component]} />
  )) }
<Router>

Each entry in the JSON would just have path, component and any additional props to pass.

@developit
Copy link
Member

Preact's website actually does this by the way, here the implementation:

https://github.com/developit/preact-www/blob/master/src/components/routes.js

@azer
Copy link
Author

azer commented Feb 2, 2017

@developit thanks a lot! I'll share the result with you once I'm done.

@azer azer closed this as completed Feb 2, 2017
@developit
Copy link
Member

Perfect, that'll make a nice addition to the docs.

@azer
Copy link
Author

azer commented Feb 5, 2017

@developit just released it; http://github.com/azer/go-preact-starter

@developit
Copy link
Member

Awesome, nice work man.

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