Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Nov 13, 2022. It is now read-only.
/reduxPublic archive

redux implementation in Go

License

NotificationsYou must be signed in to change notification settings

dannypsnl/redux

Repository files navigation

version badgesBuild Statuscodecovgo.dev referenceGitHub license

This is a redux implementation in Go.

Origin version

Install

$ go get github.com/dannypsnl/redux

Usage

import (// must import, else go module would think following package is a module    _"github.com/dannypsnl/redux/v2"// v2 store"github.com/dannypsnl/redux/v2/store"// v2 rematch(optional)"github.com/dannypsnl/redux/v2/rematch")

Basic example

// As you can see, you don't need to checking nil or not now// Now redux-go will initial the state with zero value of that typefunccounter(stateint,actionstring)int {switchaction {case"INC":returnstate+1case"DEC":returnstate-1default:returnstate    }}funcmain() {// redux/v2/storestore:=store.New(counter)store.Dispatch("INC")fmt.Printf("%d\n",store.StateOf(counter))// should print out: 1}

More stunning(lambda can work)

funcmain() {counter:=func(state,payloadint)int {returnstate+payload    }store:=store.New(counter)store.Dispatch(100)store.Dispatch(-50)fmt.Printf("%d\n",store.StateOf(counter))// should print out: 50}

Rematch

And more...

typeCountingModelstruct {    rematch.ReducerStateint}func (cm*CountingModel)Increase(s,payloadint)int {returns+payload}func (cm*CountingModel)Decrease(s,payloadint)int {returns-payload}funcmain() {c:=&CountingModel{State:0,    }store:=store.New(c)store.Dispatch(c.Action(c.Increase).With(100))store.Dispatch(c.Action(c.Decrease).With(50))fmt.Printf("result: %d\n",store.StateOf(c))// expect: 50}

Then let's have a party

typeCountingModelstruct {    rematch.ReducerStateintIncrease*rematch.Action`action:"IncreaseImpl"`}func (c*CountingModel)IncreaseImpl(s,payloadint)int {returns+payload}funcmain() {c:=&CountingModel {State:0,    }store:=store.New(c)store.Dispatch(c.Increase.With(30))store.Dispatch(c.Increase.With(20))fmt.Printf("result: %d\n",store.StateOf(c))// expect: 50}

[8]ページ先頭

©2009-2025 Movatter.jp