Skip to content

Commit

Permalink
Merge branch 'master' into add-state-to-push
Browse files Browse the repository at this point in the history
  • Loading branch information
liamqma authored Sep 13, 2023
2 parents 90e9cb9 + 2e3973f commit 5aae0f1
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 5aae0f1

Please sign in to comment.