question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

idea/help: Better route(s) defining (and reusable components continuation)

See original GitHub issue

Hi there. I’m just continue thinking about “reusable” components started from #266. And I got some structure and ideas.

So currently we can define only one time an array of routes, right? Using app.router. But why not add app.route method or something like that, to add routes more dynamically, it would be more flexible and will allow to define which component or page for which route is, inside from the component/page.

I’m thinking for the following thing

components/
- componentName/index.js
- componentName/model/
- componentName/model/index.js
- componentName/model/reducers.js
- componentName/model/effects.js
- componentName/model/state.js
pages/
- home.js
- about.js
elements/
- input.js
- button.js

Actually I don’t have real app so I can’t decide correctly, and there are some random thoughts in my head.

Description of above structure:

  • componentName/index.js would be the view also it can be that mentioned .choo file. But really we don’t need choo files, because they limiting us for no reason. We can have local state again. In that file we will just have choo/html require at least, or we can even require more elements (from elements folder) to construct some view or layout.
  • componentName/model/ - model things such as reducers and effects. It’s intentional that it is a folder. That structure is inspired by @kaktus project, where parts of the model are separated. This also allows if some view/page/other component, want to use that model - like just require('./model') (actually it won’t be recommended / correct, because they would be loaded anyway and the way they should be used is send('namespace:reducerOrEffect'))
  • componentName/model/index.js just autogenerated file which is exactly like this index - just exporting state, reducers and adds namespace. Optionally it can be just more flat if someone don’t want to separate files for the model parts.

So what’s related with the router. It’s easy. Pages in another side would be more higher-level things assembling few components. So it would be useful if any page can define it’s route, so should export function that gets app argument.

example about page

module.export = function (app) {
  // assemble few components, layouts and etc
  app.route('/about', aboutPage)
}

The second thing which may should be changed is the “plugins”, that i think it should accept function, that gets app argument and returns something. So you will have instance and would have ability to add things, routes, to register components on some cache (object or array) and etc.

So later you will have client.js like this

const choo = require('choo')
const log = require('choo-log') // updated to new plugins api
// note: this not exist, but could be wrapper for choo
const app = require('choo-components')(choo({
  hooks: 'here'
}))

app.use(log())

// register components (may not make sense? not sure)
// app.use(require('components/form'))
// app.use(require('components/foobar'))
// app.use(require('components/google-maps'))

// or directly register `pages`
app.use(require('pages/home'))
app.use(require('pages/about'))

document.body.appendChild(app.start())

It’s not very clear in my mind yet, but if we change plugins API we could do better things. And every page would just add it’s route to which it should point. Also one thing to mention is that maybe app.start should “run/start” the router (pass the app.routes array for example to app.router)


So in short, the things may look complex and complicated, but they aren’t (everything other will be in separate wrapper lib - the things about components/pages loading and generating). The needed changes is the plugins api and add app.route to add single route to app.routes array that later is passed to app.router from the app.start

edit: It is easy to just create another method like .component instead of changing plugins api, but use is good word and make sense the plugins to be more better and still allow defining hooks.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
yoshuawuytscommented, Nov 22, 2016

I started off with the idea of allowing each page to define their own routes but quickly realized that it allows hiding abstractions which makes for a good amount of complexity.

I feel what you’re proposing is different enough that it would require a full rewrite - something that I can encourage you to do, and given choo’s modular nature it should be very much possible to do.

Gonna close this for now tho, happy to hear how your experiments go!

1reaction
tunnckoCorecommented, Nov 21, 2016

Good and interesting question, but don’t think it is “framework” job to do this. The mentioned app.route would be something like app.routes.push(route) thing, it’s up to the user.

Also I don’t think it is protected from this as it is currently. If user/dev want to break his job it can. The core thing behind “frameworks” should be just thin sugar and helper things with thin borders. We don’t need strict rules.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Reuse React Components
In React, components are the main unit of code reuse. The combination-based component reuse mechanism is quite elegant, but for more ...
Read more >
Placing Reuse Component Instances on the Object Page
Each reuse component instance defined for an object page belonging to an SAP Fiori elements-based app is realized by a section.
Read more >
CS 530 - Software Engineering class notes
Object and function reuse: Small-scale software components that implement a single well-defined object or function may be reused. Benefits of software reuse:.
Read more >
Angular Route Reuse Strategy
Angular reuses components when it can but destroys them to free memory otherwise and this is the desired behavior in most cases.
Read more >
Reusable Software Components †
advances that should help make reusable software components more routinely feasible in the future. A secondary objective is to identify serious technical ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found