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.

using withState and withReducer together

See original GitHub issue

After some experimentation, it appears we can chain withState after withReducer to set initial state. If I chain withState before withReducer, then that initial state is discarded. However, I’m wondering if this behavior is a feature we should be using or we really should only use withReducer and pass in the initial state? If we can use both withState and withReducer, it would be nice if ordering did not matter similar to how the .put() works. .put() just expects a put() will occur at some point in the saga.

I find myself naturally gravitating to a syntax where I use withState and withReducer b/c it explicitly tells me what’s going on (that is: I’m using these reducers and this state). Whereas, passing two args into withReducer is less explicit.


      expectSaga(someSaga)
        /// does not work!  this initial state will be discarded after withReducer
        // would be nice if ordering did not matter
        .withState({
          reducer1: {},
        })
        .withReducer(combineReducers({
          reducer1,
        }))
        .run();

      // declarative and and clear what's going on - imo
      expectSaga(someSaga)
        .withReducer(combineReducers({
          reducer1,
        }))
        .withState({
          reducer1: {},
        })
        .run();

      // a little vague - imo
      expectSaga(someSaga)
        .withReducer(combineReducers({
          reducer1,
        }), {
          reducer1: {},
        })
        .run();

Any thoughts?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tastafurcommented, May 24, 2018

This works, try to establish the state of the reducer after adding the reducer? .withReducer(reducer) .withState(state) @jfairbank

1reaction
jfairbankcommented, Feb 17, 2018

Hi, @epikhighs.

I initially introduced withState before withReducer, so that’s part of the reason why they don’t play nice together. I’m not opposed to changing the behavior, but I still think having the implicit initial state from a reducer would be useful. Maybe if initial state wasn’t set withwithState, then we could use the implicit initial state from the reducer.

I’m happy to accept a PR that changes the behavior to let the two methods cooperate more nicely.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scaling Up with Reducer and Context - React Docs
Context lets you pass information deep down to other components. You can combine reducers and context together to manage state of a complex...
Read more >
Using Recompose to Share Functionality Between React ...
Recompose provides you with the withState() helper to add state to your functional stateless components. It manages a single state value. You ...
Read more >
State · GitBook - Redux Saga Test Plan
State. You can test your saga's integration with your reducer and store state via the withState , withReducer , and hasFinalState methods.
Read more >
React Hooks vs. Redux: Do Hooks and Context replace Redux?
Fortunately, the React Context API solves this problem. When combined with React Hooks, we have a state management solution that is less time- ......
Read more >
Examples of the useReducer Hook - Dave Ceddia
(We could also manage the input with state, passing the value and onChange props as usual, but this is a good chance to...
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