Skip to content

Commit

Permalink
Added docs on how to created nested routes with a slot (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderKaran authored Sep 12, 2023
1 parent 25266bf commit 2e3973f
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
- [Adding](./adding.md)
- [Usage](./usage.md)
- [Interaction](./interaction.md)
- [Nested Routes](./nested-routes.md)
45 changes: 45 additions & 0 deletions docs/resources/nested-routes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# How to Create Nested Routes Using Slot in React Resource Router

The React Resource Router library generally encourages you to keep your routing flat. However, there may be situations where you need nested components associated with different URLs. To accomplish this, you can create a Slot component.

Here's how to do it:

## Slot Component

```js
import { useRouter, Redirect } from 'react-resource-router';

// Define a object of URLs to Components.
// Make sure the keys in this object match the URLs you intend to handle.
const slots = {
'url-one': ComponentOne,
'url-two': ComponentTwo,
};

const Slot = () => {
const [{ route }] = useRouter();
const name = route.name;

// If the specified slot doesn't exist, redirect to a 404 page
if (!slots?.[name]) {
return <Redirect to="/404" />;
}

// Dynamically load and render the component based on the current route name
const DynamicComponent = slots[name];
return <DynamicComponent />;
};

```
## Add Slot Component to Parent
```js
const Page = () => {
return (
<div>
<Slot>
</div>
)
}
```

0 comments on commit 2e3973f

Please sign in to comment.