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

Pass context to render function #243

Merged
merged 2 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,14 @@ A class that represents an application. An application is responsible for render
```js
const app: App = new App(
(el: ReactElement),
(render: ?(el: ReactElement) => any)
(render: ?(el: ReactElement, ctx: Context) => any)
);
```

- `el: ReactElement` - a template root. In a React application, this would be a React element created via `React.createElement` or a JSX expression.
- `render: ?Plugin<Render>|Render` - Optional. Defines how rendering should occur. A Plugin should provide a value of type `Render`
- `type Render = (el:any) => any`
- `type Render = (el:ReactComponent, ctx: Context) => any`


**app.register**

Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ import App from '../index';

test('custom render function', async t => {
let didRender = false;
const app = new App(React.createElement('span', null, 'hello'), () => {
const app = new App(React.createElement('span', null, 'hello'), (el, ctx) => {
t.ok(el);
t.ok(ctx);
didRender = true;
return 10;
});
Expand Down
14 changes: 10 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
/* eslint-env browser */
import * as React from 'react';

import FusionApp, {createPlugin, CriticalChunkIdsToken} from 'fusion-core';
import FusionApp, {
createPlugin,
CriticalChunkIdsToken,
type Context,
} from 'fusion-core';
import {prepare} from './async/index.js';
import PrepareProvider from './async/prepare-provider';
import {registerInjector, withServices} from './injector.js';
Expand All @@ -21,10 +25,12 @@ import ProviderPlugin from './plugin';
import ProvidedHOC from './hoc';
import Provider from './provider';

export type Render = (el: React.Element<*>, context: Context) => any;

declare var __NODE__: Boolean;

export default class App extends FusionApp {
constructor(root: React.Element<*>, render: ?(React.Element<*>) => any) {
constructor(root: React.Element<*>, render: ?Render) {
if (!React.isValidElement(root))
throw new Error(
'Invalid React element. Ensure your root element is a React.Element (e.g. <Foo />) and not a React.Component (e.g. Foo)'
Expand All @@ -34,10 +40,10 @@ export default class App extends FusionApp {
criticalChunkIds: CriticalChunkIdsToken.optional,
},
provides() {
return (el: React.Element<*>) => {
return (el: React.Element<*>, ctx) => {
return prepare(el).then(() => {
if (render) {
return render(el);
return render(el, ctx);
}
if (__NODE__) {
return serverRender(el);
Expand Down