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

Document Route wrapper for typescript #465

Merged
merged 4 commits into from
Apr 6, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ render(<Main />, document.body);

If there is an error rendering the destination route, a 404 will be displayed.

#### Caveats

Because the `path` and `default` props are used by the router, it's better to avoid
same props in your components.
rschristian marked this conversation as resolved.
Show resolved Hide resolved

### Handling URLS

:information_desk_person: Pages are just regular components that get mounted when you navigate to a certain URL.
Expand Down Expand Up @@ -276,6 +281,38 @@ function App() {
render(<App />, document.body);
```

### Typescript Routes

In some cases, depending on your TypeScript configuration, you may encounter
an error that prevents you from passing the `path` or `default` props to your components:

```tsx
error TS2322: Type '{ path: string; myProps: MyProps }' is not assignable to type 'IntrinsicAttributes & MyProps'.
Property 'path' does not exist on type 'IntrinsicAttributes & MyProps'.

<MyComponent path="/component"
~~~~
```

In such cases, you can wrap your components in `<Route />`. This will also accept your component's
props. Keep in mind that both `path` and `default` props are passed down to your component.

```tsx
import {Router, Route} from 'preact-router';

function App() {
let users = getUsers();

return (
<Router>
<Route component={Home} path="/" />
{/* Route will accept any props of `component` type */}
<Route component={Users} users={users} path="/users" />
</Router>
);
}
```

zlondrej marked this conversation as resolved.
Show resolved Hide resolved
### License

[MIT](./LICENSE)