Comments (11)
Could switch be made official? Sometime you want to transition routes in a central place as opposed to individual transitions, for instance: https://codesandbox.io/embed/jp1wr1867w
from wouter.
@drcmda I plan to introduce the <Switch />
component in version v1.2.0
which hopefully will take place later this week. When I was designing the library my initial thought was: if a route will always match the entire path (like with exact
option always on), there is no need for a switch. However, I must admit there are cases like this one:
<Switch>
<Route path="/users/home">All users</Route>
<Route path="/users/:id">User's Page</Route>
</Switch>
Where switch is only way to do a proper exclusive routing.
So, I've adapted your sandbox and made a PoC with the Switch: https://codesandbox.io/s/w581z5xp5
from wouter.
Hey @nkov thanks for trying the library out!
I recreated your example in Code Sandbox here and looks like there are two reasons why this doesn't work:
- When there is no match,
Switch
returns undefined, which makes React throw an error. I fixed that by checking this edge case and returningnull
. - The bigger problem is that
useRouter
hook does not make a component rerender in case if location changes. Fortunately, there is another hook for thatuseLocation
. It returns a tuple of[location, setLocation]
which basically allows to manage current location as if it was a part of the state.
I made another version of the component, so check this out. It's kinda similar to the original Switch
implementation from React Router package. Let me know what you think.
I also thought that it would be nice to have switch around it the library, so I might end up including it in the sources. I wasn't sure if people use Switch a lot, but seems like they do. Anyways, curious to hear back from you!
from wouter.
The only thing im asking for is that we need to be able to forward a location (or whatever it is called here) to that switch, which will allow transitions to fade out old history states safely.
<Switch location={...}>
<Route path="/users/home">All users</Route>
<Route path="/users/:id">User's Page</Route>
</Switch>
If you're done i would love to make a react-spring codesandbox for wouter. 🙂
from wouter.
@molefrog ah, did not know about this hook, maybe it would be good to also add to the readme with some small description. As far as adding the <Switch>
component to the library, I think that's a great idea. Adding a <Redirect>
which can be created using the hook as well, as you demonstrated on Reddit, might be nice too. It's not difficult to implement each time but it's nice to have it as a component anyway.
from wouter.
Hey, @nkov @drcmda just pushed a PR with Switch and Redirects implemented #9. Let me know what you think!
from wouter.
Looks great! If it's merged could you ping me?
from wouter.
@drcmda Okay, perfect. Gonna merge this right away and let you know when the new version is out, so you can recreate the transition demo.
from wouter.
@drcmda I've released version v1.2.0 of the library with the new Switch
component include. Here is an updated demo (https://codesandbox.io/s/w581z5xp5) with react-spring transitions.
from wouter.
I did one too https://codesandbox.io/s/oo4ox81q5 but still struggle with performance, two react reconcilers in there and too many elements, it's my fault not wouters. :( But wouter is doing great, i love the fact there's no context in it, that allows it to work across reconcilers, like nothing else really does. I'll try to fix it up some more and tweet this out soon.
from wouter.
Looks great, thanks.
from wouter.
Related Issues (20)
- Router don't work with Preact (10.18.1) HOT 3
- wouter-preact: Params with typescript? HOT 2
- How to add params in a nesting route? HOT 3
- v2.12.1 npm package is out of sync with v2.12.1 tag HOT 1
- v3.0.0-rc.1: When use hash router, Ctrl+click on <Link>, new window is opened with wrong url HOT 3
- Potential `useSearch` bug not triggering updates when used with `urql` HOT 9
- [Bug] Can't match default <Route>Not found</Route> when Router inside Switch HOT 3
- (?) Hash routing with no prefix slash index.html#/example/path/ HOT 3
- useSearch unsafe parameter decoding HOT 3
- v3.0.0-rc.2 decodeUriComponent is called before route match, breaking links with urlencoded slash (%2F) HOT 2
- v3.0.0-rc.1: `useRoute` should allow `nest` option just like `<Route />` HOT 1
- Suggestion: `<Link>` should always render an `<a>` (or at least have the option to do so) HOT 5
- Accessing nested params with `useParams` HOT 8
- Nested routing broken by intermediate component HOT 2
- Exit animation with framer-motion HOT 7
- How to use V3 with Jest? HOT 3
- [BUG] `useHashLocation` not syncing with state HOT 3
- Docs don't say how to install; no readme on NPM page HOT 1
- import/no-unresolved eslint error being raised on Wouter 3.0.x HOT 3
- Intercept location change with custom hook swallows state HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wouter.