Comments (5)
@Ieremchuk I think you could get by using hoisting (i.e. define the store above, pass the reference to the createRoutes, and then set the result of compose) if you wanted, but there is a far easier approach.
You don't have to pass the routes to the redux-router
middleware and I think its even discouraged when using server-side rendering. Check out the SSR example in the redux-router
repo here.
The important bit is:
const store = compose(
reduxReactRouter({ createHistory }),
devTools()
)(createStore)(reducer, window.__initialState);
const rootComponent = (
<Provider store={store}>
<ReduxRouter routes={routes} />
</Provider>
You can pass the routes to the ReduxRouter component instead of the middleware and it will load them properly.
Let me know if that solves your issue
from redux-auth-wrapper.
@mjrussell Thanks for your answer. Could you please add a little bit more about "hoisting" approach( any google keys or link to documentation )? I am not an experienced js developer.
Regarding the easier approach. I saw this example. It contains a server part as well (link) .
And this part is a main problem for me, because it uses the routes.
app.use((req, res) => {
const store = reduxReactRouter({ routes, createHistory: createMemoryHistory })(createStore)(reducer);
const query = qs.stringify(req.query);
const url = req.path + (query.length ? '?' + query : '');
// skiped
from redux-auth-wrapper.
@Ieremchuk sorry for not looking through the example more thoroughly...the trick with this approach is "abusing" how the references work in javascript. Instead of capturing scoped variables in a closure, references always refer back to the variables. For example:
let x;
function y() {
return x + 1;
}
y() // returns NAN
x = 1;
y() // returns 2
So in your case you could do something like:
let store;
const routes = createRoutes(store);
store = reduxReactRouter({ routes, createHistory: createMemoryHistory })(createStore)(reducer);
The connectRedirect only needs the store when the onEnter
fires, which is long after the store has been created.
from redux-auth-wrapper.
@mjrussell It doesn't work for me. By some reason the store remains undefined inside the connect.
const getRoutes = (store) => {
console.log(store); // undefined
let connect = function connect(fn) {
return function (nextState, replaceState) {
console.log(store); // still left undefined when the top level store has been already created
return fn(store, nextState, replaceState);
};
};
Looks like something wrong with closure. I will try to figure out.
Thanks for your efforts and time.
from redux-auth-wrapper.
@mjrussell Solution that works for me.
- move connect outside the getRouts.
const getRoutes = (connect) => {
return (
<Route>
<Route path="/" component={App}>
<Route path="login" component={Login}/>
<Route path="foo"
component={UserIsAuthenticated(Foo)}
onEnter={connect(UserIsAuthenticated.onEnter)}
/>
</Route>
</Route>
);
};
- And define connect in global context that allows to capture the store in closure.
let store;
const connect = (fn) => (nextState, replaceState) => fn(store, nextState, replaceState);
const routes = createRoutes(connect);
store = reduxReactRouter({ routes, createHistory: createMemoryHistory })(createStore)(reducer);
All works as expected.
from redux-auth-wrapper.
Related Issues (20)
- [Possible Bug] Async Validation in a multi-step form HOT 1
- How to make chunk js with react-router HOT 1
- Error in UserAuthWrapper
- Hande logout when expired token in axios interceptors but not redirect to login page HOT 2
- Misleading naming of authenticatedSelector HOT 2
- Example with react 16.x HOT 1
- Reducer not yet defined HOT 2
- Usage with react-cookies
- not running when am trying do npm start HOT 1
- connectedReduxRedirect is missing HOT 1
- Deprecated lifecycle method in redirect.js HOT 7
- Doesn't work with latest react HOT 3
- Type {} is missing the following properties from type Readonly<InjectedAuthProps> HOT 1
- ES6 Module - Error when using with Electron HOT 2
- redirect.js replace UNSAFE_componentWillMount with componentDidMount HOT 6
- No question mark befor query param on redirect HOT 2
- Versioning Order HOT 4
- React-router v6 HOT 9
- Infinite loop (redirect ) with nesting wrappers HOT 1
- Call for mantainers! HOT 4
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 redux-auth-wrapper.