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

Layout pages #47

Closed
Lucanis opened this issue Jan 17, 2018 · 11 comments
Closed

Layout pages #47

Lucanis opened this issue Jan 17, 2018 · 11 comments

Comments

@Lucanis
Copy link

Lucanis commented Jan 17, 2018

Nice work, this looks really promising.

I have a small question, is there a way to define a layout, to reuse a component on each page (e.g., a navigation component) ?

@StevenLangbroek
Copy link

StevenLangbroek commented Jan 17, 2018

It's React, so you don't need anything specific from after.js..., i.e.:

layout.js:

import React from 'react';
import Navigation from './Navigation';

export default function Layout(props) {
  return (
    <div>
      <Navigation />
      <div className="main">
        {props.children}
      </div>
    </div>
  );
}

then in your page just do:

function MyPage(props) {
  return (
    <Layout>
      <h1>My page!</h1>
    </Layout>
  );
}

@jaredpalmer
Copy link
Owner

jaredpalmer commented Jan 17, 2018

How would ppl feel about a top level Root component from which all routes inherit? Right now this is _app.js which is part of After. However, we could allow users to write their own.

@StevenLangbroek
Copy link

Could this also help with misc <Provider>s?

@jaredpalmer
Copy link
Owner

jaredpalmer commented Jan 17, 2018

Yes. Unlike next.js, where you need to keep around a lot of HoC's to accomplish this (e.g. withData()), with this kind of setup you could wrap things on your own.

Route Config

// ./src/_routes.js

// ...imports

export default [{
   path: '/',
   component: Root, // providers could go here
   routes: [
       {
         path: '/',
         component: Home,
         exact: true
       },
       {
         path: '/about',
         ...
       }
   ]
}]

However, this wouldn't be perfect--imagine if you need to create a different router than BrowserRouter or you want to wrap your entire client app (including the router) in a provider. For example, this solution won't work with react-router-redux. https://github.com/ReactTraining/react-router/tree/master/packages/react-router-redux

@jaredpalmer
Copy link
Owner

I wonder if it makes sense allow users to copy and paste _client.js. This was my initial idea. Seems much better than trying to find a "framework" solution.

@frol
Copy link

frol commented Jan 18, 2018

@jaredpalmer Should the demonstrated router config work? My small experiment ended up with only Root component, and anything that is in the nested routes just seems to be ignored.

@StevenLangbroek
Copy link

@frol did you remember to pass props.children through?

@frol
Copy link

frol commented Jan 18, 2018

Here is the props in the Root component:

screenshot from 2018-01-18 15-34-13

(NOTE: no children)

import React from 'react'

export default class extends React.PureComponent {
  render() {
    console.log(this.props)
    return (
        <div>
            <h1>Head</h1>
            {this.props.children}
            <h1>Tail</h1>
        </div>
    )
  }
}

@pronevich
Copy link
Contributor

I think "layout" components are uncovered issue in ssr land and after.js has the chance to change this in the "right" way:

  1. Don't import same layout component on each page. Sometimes layout, not the page, defines route point. Then pages go under the layout. Something that @jaredpalmer proposed:
export default [{
   path: '/',
   component: Root, //  providers could go here and RootLayout if needed 
   routes: [
       {
         path: '/',
         component: Home, // will be under RootLayout
         exact: true
       },
       {
         path: '/user',
         component: UserLayout,
       },
       {
         path: '/user/login',
         component: LoginComponent // will be under UserLayout
       },
       ...
   ]
}]
  1. Layout can be split point too. import('UserLayot')
  2. On next route under the same layout, we loading only PageComponent, because of 1.

@felipeleusin
Copy link

Is there a reason for this issue been closed? Also got hit with this but couldn't find a fix in the source. We were thinking about how to fix it and the one that makes most sense specially considering the motto of "what if next.js and react-router had a baby" was to be able to use nested routing like react-router-config. Are you open to a PR using it?

Otherwise I can think of being able to send a Layout prop to render that gets injected into the After (afterparty) component and it defaults to Fragment. Then we can just include this component again in client.js

@alexparish
Copy link

@jaredpalmer What's the current advice on setting up a shared layout page?

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

7 participants