You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `createBrowserRouter` function takes an options object. The only mandatory property on this object is `routeConfig`, which should be a route configuration as above.
446
+
`createBrowserRouter` takes an options object. The only mandatory property on this object is `routeConfig`, which should be a route configuration as above.
446
447
447
448
The options object also accepts a number of optional properties:
448
449
@@ -466,7 +467,7 @@ The created `<BrowserRouter>` accepts an optional `matchContext` prop as describ
466
467
467
468
#### `createFarceRouter`
468
469
469
-
`createFarceRouter` exposes additional configuration for customizing navigation management and route element resolution. To enable minimizing bundle size, it omits the defaults from `createBrowserRouter`.
470
+
`createFarceRouter` exposes additional configuration for customizing navigation management and route element resolution. To enable minimizing bundle size, it omits some defaults from `createBrowserRouter`.
The options object for `createFarceRouter` should have a `historyProtocol` property that has a history protocol object. For example, to use the HTML History API as with `createBrowserRouter`, you would provide `new BrowserProtocol()`.
499
498
500
-
The `createFarceRouter` options object does not have defaults for the `historyMiddlewares` and `render`properties. It ignores the `renderPending`, `renderReady`, and `renderError` properties.
499
+
The `createFarceRouter` options object does not have a default for the `render`property. It ignores the `renderPending`, `renderReady`, and `renderError` properties.
501
500
502
501
The created `<FarceRouter>` manages setting up and providing a Redux store with the appropriate configuration internally. It also requires a `resolveElements` prop with the route element resolution function. For routes configured as above, this should be the `resolveElements` function in this library.
When creating a store for use with the created `<ConnectedRouter>`, you should install the `foundReducer` reducer under the `found` key. You should also use a store enhancer created with `createHistoryEnhancer` from Farce, and a store enhancer created with `createMatchEnhancer`, which must go after the history store enhancer. `createMatchEnhancer` takes a matcher object which handles the actual route matching. Dispatch `FarceActions.init()` after setting up your store to initialize the event listeners and the initial location for the history store enhancer.
562
+
When creating a store for use with the created `<ConnectedRouter>`, you should install the `foundReducer` reducer under the `found` key. You should also use a store enhancer created with `createHistoryEnhancer` from Farce and a store enhancer created with `createMatchEnhancer`, which must go after the history store enhancer. Dispatch `FarceActions.init()` after setting up your store to initialize the event listeners and the initial location for the history store enhancer.
566
563
567
-
`createConnectedRouter` ignores the `historyProtocol`, `historyMiddlewares`, and `historyOptions` properties on its options object. It requires the `matcher` property with the matcher object used in creating the match enhancer.
564
+
`createConnectedRouter` ignores the `historyProtocol`, `historyMiddlewares`, and `historyOptions` properties on its options object.
568
565
569
566
`createConnectedRouter` also accepts an optional `getFound` property. If you installed `foundReducer` on a key other than `found`, specify the `getFound` function to retrieve the reducer state.
570
567
@@ -678,7 +675,7 @@ If you want to run your transition hooks when the user attempts to leave the pag
678
675
679
676
The [transition hook usage example](/examples/transition-hook) demonstrates the use of transition hooks in more detail, including the use of the `useBeforeUnload` option.
680
677
681
-
####Redux integration
678
+
### Redux integration
682
679
683
680
Found uses Redux to manage all serializable state. Farce uses Redux actions for navigation. As such, you can also access those serializable parts of the routing state from the store state, and you can navigate by dispatching actions.
`getFarceResult` takes an options object. This object must include the `url` property that is the full path of the current request, along with the `routeConfig` and `render` properties needed to create a Farce router component class normally.
743
+
744
+
The options object for `getFarceResult` also takes the `historyMiddlewares` and `historyOptions` properties, as above for creating Farce router component classes. This options object also takes optional `matchContext` and `resolveElements` properties, as described above as props for router components. `resolveElements` defaults to the standard `resolveElements` function in this library.
745
+
746
+
`getFarceResult` returns a promise for an object with the following properties:
747
+
748
+
-`redirect`: if present, indicates that element resolution triggered a redirect; `redirect.url` contains the full path for the redirect location
749
+
-`status`: if there was no redirect, the HTTP status code for the response; this will be `error.status` from any encountered `HttpError`, or 200 otherwise
750
+
-`element`: if there was no redirect, the React element corresponding to the router component on the client
751
+
752
+
This promise resolves when all asynchronous dependencies are available. If your routes require asynchronous data, e.g. from `getData` methods, you may want to dehydrate those data on the server, then rehydrate them on the client, to avoid the client having to request those data again.
753
+
754
+
#### Server-side rendering with custom Redux store
755
+
756
+
If you are using server-side rendering, you will need to delay the initial render on the client. In this case, use `createInitialBrowserRouter` or `createInitialFarceRouter` instead of `createBrowserRouter` or `createFarceRouter` respectively.
These behave similarly to their counterparts above, except that the options object for `createInitialBrowserRouter` requires a `render` method, and ignores the `renderPending`, `renderReady`, and `renderError` properties. Additionally, these functions take the initial `matchContext` and `resolveElements` if relevant as properties on the options object, rather than as props.
777
+
778
+
Found exposes lower-level functionality for doing server-side rendering for use with your own Redux store, as with `createConnectedRouter` above. On both the server, use `getStoreRenderArgs` to get a promise for the arguments to your `render` function, then wrap the rendered elements with a `<RouterProvider>`.
You must dispatch `FarceActions.init()` before calling `getStoreRenderArgs`. `getStoreRenderArgs` takes an options object. This object must have the `store` property for your store and the `resolveElements` property as described above. It supports an optional `matchContext` property as described above as well. `getStoreRenderArgs` returns a promise that resolves to a `renderArgs` object that can be passed into a `render` function as above.
822
+
823
+
`<RouterProvider>` requires a `router` prop that is the router context object as described above. This is available on `renderArgs.router`, from the value resolved by `getStoreRenderArgs`.
824
+
825
+
On the client, pass the value resolved by by `getStoreRenderArgs` to your `<ConnectedRouter>` as the `initialRenderArgs` prop.
826
+
827
+
```js
828
+
import { getStoreRenderArgs } from'found';
829
+
830
+
/* ... */
831
+
832
+
(async () => {
833
+
/* ... */
834
+
835
+
constinitialRenderArgs=awaitgetStoreRenderArgs({
836
+
store,
837
+
matchContext,
838
+
resolveElements,
839
+
});
840
+
841
+
ReactDOM.render(
842
+
<Provider store={store}>
843
+
<ConnectedRouter
844
+
matchContext={matchContext}
845
+
resolveElements={resolveElements}
846
+
initialRenderArgs={initialRenderArgs}
847
+
/>
848
+
</Provider>,
849
+
document.getElementById('root'),
850
+
);
851
+
})();
852
+
```
853
+
702
854
### Minimizing bundle size
703
855
704
856
The top-level `found` package exports everything available in this library. It is unlikely that any single application will use all the features available. As such, for real applications, you should import the modules that you need from `found/lib` directly, to pull in only the code that you use.
0 commit comments