z-pattern-matching / z

Pattern Matching for Javascript
https://z-pattern-matching.github.io/
Apache License 2.0
1.72k stars 50 forks source link

using Z with redux? #46

Closed MarcCoquand closed 5 years ago

MarcCoquand commented 6 years ago

Hey so I'm testing out the possibility of using z with the reducer in redux with the following code:

export const reducer = (state, action) =>
  matches(action.type)(
    (x = 'ADD') => ({...state, counter: state.counter+1}),
    (x = 'REM') => ({...state, counter: state.counter-1}),
    x => {console.log(`no state ${x}`); return state}
  );

and then in then in my main app (a counter) I have the following:

const Counter = ({value, onIncrement, onDecrement}) =>
  <div>
    <button onClick={onIncrement}>
      +
    </button>
    {value}
    <button onClick={onDecrement}>
      -
    </button>
  </div>

const App = ({state, dispatch}) =>{
  console.log(state)
  return (<div>
    <Counter value={state.counter} onDecrement={() => dispatch({type: "REM"})} onIncrement={() => dispatch({type: "ADD"})}/>
  </div>)
}
export default connect(state => ( {state: state} ))(App);

but when I click a button returns no state REM, in the stack trace I saw that subjectToMatch = "REM".

leonardiwagner commented 6 years ago

Are you using Babel? This is probably related to Babel since it transpiles the matches so it doesn't work well with z , if you want to use Babel (or any other transpiler) with z is necessary to use the js default parameters at least, using babel stages presets will make it works!

leonardiwagner commented 5 years ago

closing due to lack of feedback