easoncxz / twitanalysis

Dig your Twitter data
https://easoncxz.github.io/twitanalysis
Other
1 stars 0 forks source link

Set up some routing for React-Redux #15

Closed easoncxz closed 4 years ago

easoncxz commented 4 years ago

I don't want to put everything on the same page.

Found this blogpost introducing a demo library with appropriate ideology (i.e. "all state is kept in the store"):

Also seeming legit:

Testing protocol:

Given an app set up with redux-router,

Possibly relevant:

~Maybe best to just use react-router directly first, and worry about managing the location state in Redux later.~

easoncxz commented 4 years ago

With some tweaking around with peer dependencies of connected-react-router, and sucking it up and using Provider from react-redux, I have things working:

image

It is decent, because I obtained the rendered route info from the Redux state:

<Switch>
  <Route exact path="/" render={app} />
  <Route
    exact
    path="/other"
    render={() => (
      <div>
        <p>Another path</p>
        <p>The current location is:</p>
        <pre>{pretty(store.getState().router.location)}</pre>
        <button onClick={() => store.dispatch(ConnRouter.push('/'))}>
          Go home
        </button>
      </div>
    )}
  />
</Switch>

The things that connected-react-router forces me to do with my top-level state/Model and action/Msg types are quite hideous, but hey at least it works:


type ModelAndRouter = Redux.CombinedState<{
  model: Model;
  router: ConnRouter.RouterState<unknown>;
}>;

type MsgOrRouter = Msg | ConnRouter.RouterAction<unknown>;

const hist = createHashHistory();

const store: Redux.Store<ModelAndRouter, MsgOrRouter> = Redux.createStore(
  Redux.combineReducers({
    model: reducer,
    router: ConnRouter.connectRouter(hist),
  }),
  undefined,
  Redux.compose(Redux.applyMiddleware(ConnRouter.routerMiddleware(hist))),
);
easoncxz commented 4 years ago

Further developments: I figured out how to get just the history package to do what I need. It's much simpler in terms of both concepts and APIs compared to the combination of:

See docs for history here:

And my commit that implements this: