Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to connect non component Class to redux store?

So I am using a react-redux boilerplate that has an ApiClient helper. It looks like this:

export default class ApiClient {
  constructor(req) {
    /* eslint-disable no-return-assign */
    methods.forEach((method) =>
      this[method] = (path, withCredentials, { params, data } = {}) => new Promise((resolve, reject) => {

        const request = superagent[method](formatUrl(path))

        if (withCredentials) {
          console.log('first of all, its true')
          console.log(this)
        }

        if (params) {
          request.query(params)
        }

        if (__SERVER__ && req.get('cookie')) {
          request.set('cookie', req.get('cookie'))
        }

        if (data) {
          request.send(data)
        }

        request.end((err, { body } = {}) => {

          return err ? reject(body || err) : resolve(body)

        })
      }))
    /* eslint-enable no-return-assign */
  }
  /*
   * There's a V8 bug where, when using Babel, exporting classes with only
   * constructors sometimes fails. Until it's patched, this is a solution to
   * "ApiClient is not defined" from issue #14.
   * https://github.com/erikras/react-redux-universal-hot-example/issues/14
   *
   * Relevant Babel bug (but they claim it's V8): https://phabricator.babeljs.io/T2455
   *
   * Remove it at your own risk.
   */
  empty() {}
}

I want to connect this to my auth so that I can prepend headers to protected endpoints, like so:

@connect(state => ({ jwt: state.auth.jwt }))
export default class ApiClient {
  ...

However, when I do this, I get the error: Cannot read property 'store' of undefined. What's going on here? Why can't I connect a regular Class to the redux store?

Update: here's my login function, which uses the ApiClient helper:

export function loginAndGetFullInto(email, password) {
  return dispatch => {
    return dispatch(login(email, password))
    .then(() => {
      return dispatch(loadUserWithAuth())
    })
  }
}

I need some way to either pass the store, or the jwt, into the loadUserWithAuth function...

like image 423
j_d Avatar asked Nov 28 '16 15:11

j_d


People also ask

Can we access Redux store outside of component?

If you need to dispatch actions from outside a React component, the same technique will work: import the store, then call store. dispatch() , passing the action you need to dispatch. It works the same as the dispatch function you get from props via react-redux's connect function.

How can I use the Redux store in Non component files?

To access the store in non-component front end code, I used the analogous import (i.e., import {store} from "../../store. js" ) and then used store. getState() and store. dispatch({*action goes here*}) to handled retrieving and updating (er, sending actions to) the store.

How do I connect to Redux store?

The connect() function connects a React component to a Redux store. It provides its connected component with the pieces of the data it needs from the store, and the functions it can use to dispatch actions to the store.


1 Answers

The connect function is not going to work with anything other than React components. What you can do is pass your store instance to your class and call store.dispatch, store.getState, and store.subscribe directly.

Keep in mind that if you subscribe, you should also have functionality to unsubscribe, otherwise your store will be holding a reference to your class instance forever, creating a memory leak.

like image 152
Jim Bolla Avatar answered Oct 19 '22 02:10

Jim Bolla