question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Easy way to map getState() to this.props.getState() for dependency injection?

See original GitHub issue

Hello.

I’m working on a piece of code called “reduxify” which I hope to publish as an NPM module, that helps take away some of the boilerplate of using react-redux on large projects.

The problem that I’m running into is that I can’t seem to find a way to map getState() to props. This is somewhat important for the design pattern I’m working with.

Specifically, in order to keep my “utilities” unit-testable in isolation - that is, without having to import the store, I was using dependency injection. That is, if I had a file called ./utilities/api.js, it would be written something like this:

export default function api = (dispatch, getState) => {
  const getRandomNumber = (req) => {
    request(req).then((num) => {
       dispatch(addNumber(num));
       let currentState = getState(); 
       if (currentState.number % 2 === 0 ){
         dispatch(evenAction())
       } else {
         dispatch(oddAction()); 
       }
    })
  }
  return {
     getThingy,
  }
}

This way, I can mock both dispatch and getState. As for the use in dev/production, it would be something like this:

import apiRoot from './api'

class NumberThing extends Component{
  constructor(props){
     let api = apiRoot(this.props.dispatch, this.props.getState)
  }
  this.handleClick(){
     api.getThingy(this.state.whatever)
  }
}

I’ll be writing v. 1 of Reduxify without this functionality, but I’m thinking there’s gotta be a way to do this without exporting getState() from the store and importing it into the component.

Your thoughts?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Jun 26, 2016

Hi, Brian. I know we talked about something like this a while back. Is there any reason you aren’t just using thunks? Because thunks are middleware-based, the thunk functions receive getState and dispatch automatically. Also, with the newer redux-thunk v2.1.0, you can automatically inject additional arguments into your thunk functions, such as specific AJAX API wrappers or something.

0reactions
kerryboykocommented, Jun 27, 2016

Only that I’ve tried it, and it doesn’t work for whatever reason. I suspect it is because Redux Dev Tools attempts to recalculate the state from scratch by running through all the actions, every time you move backwards in the timeline, which is problematic for an application that can have up to 3000 actions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can I pass the .getState() method in as a prop to my ...
This can be solved using React's context, which is essentially a dependency injection mechanism and allows you to send prop-like values ...
Read more >
Connect: Extracting Data with mapStateToProps - React Redux
The first argument to a mapStateToProps function is the entire Redux store state (the same value returned by a call to store.getState() )....
Read more >
How To Manage State on React Class Components
Start by creating a component with no state. The component will have two parts: The cart, which has the number of items and...
Read more >
Using state from your Store in your views - Human Redux
This function will receive the entire application state object as returned by calling store.getState(); You have to return an object. Each key of...
Read more >
React Redux
the state parameter is connect calling store.getState() and returning the result as state function mapStateToProps(state) { return { counter: state.counter, } ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found