frontarm / navi

🧭 Declarative, asynchronous routing for React.
https://frontarm.com/navi/
MIT License
2.07k stars 71 forks source link

How to access Redux state from navi? #175

Closed OnkelTem closed 4 years ago

OnkelTem commented 4 years ago

Hi!

Sorry if my question may seem naive, but I'm not sure how can I access my redux state from inside my navi erm.. contraptions?

E.g. this is a part of my App component:

export default function App() {
  return (
    <ReduxProvider store={store}>
      <Router routes={routes}>
        <Suspense fallback={null}>
          <View />
        </Suspense>
      </Router>
    </ReduxProvider>
  );
}

and this is a part of my routes:

  mount({
    // ...
    "/dashboard/:inputId": route({
      title: "Input",
      getData: (request) => {
        /*---- Here I need to access state.something ----*/
      },
      getView: (request) => {
        const inputId = parseInt(request.params.inputId);
        return <InputPage inputId={inputId}/>
      }
    }),
  })

I would like to access my Redux store from the /*---- Here I need to access state.something ----*/ line. Any ideas?

kettanaito commented 4 years ago

Hi, @OnkelTem. You can always access the Redux store via store.getState().

import store from '../where/you/define/store'

mount({
  "dashboard/:inputId": route({
    getData(request) {
      const state = store.getState()
      return state.path.value
    }
  })
})

Read more about the store API in Redux docs.