Fork me on GitHub
#helix
<
2020-11-11
>
geraldodev00:11:39

@lilactown How do you pass parameter to the reducer function besides the action ?

lilactown01:11:45

i'm not sure what you mean yet. the reducer function signature is:

(fn [state action] ,,,)

lilactown01:11:46

example:

(defn my-reducer
  [state action]
  (case (:type action)
    :foo (update state :foo inc)
    :bar (update state :bar inc)))

(defnc my-component
  (let [[state dispatch] (hooks/use-reducer my-reducer {:foo 0 :bar 0})]
    (d/div
      (d/div "Foo: " (d/button {:on-click #(dispatch {:type :foo})} (:foo state)))
      (d/div "Bar: " (d/button {:on-click #(dispatch {:type :bar})} (:bar state))))))

👍 1
ordnungswidrig15:11:07

The action can by any value. As in the example a map which can also contain “parameters” besides the “action”.

👍 1