myitcv / x

Mono-repo for all myitcv.io/... Go code
MIT License
103 stars 20 forks source link

react: question about state changes #52

Closed myitcv closed 6 years ago

myitcv commented 6 years ago

Straight link/copy of https://github.com/myitcv/react/issues/164 now that everything has moved to the mono-repo

cc @pjebs

myitcv commented 6 years ago

@pjebs

In your todo sample application, you have a Equal( ) function attached to the state.

Specifically we're talking about https://github.com/myitcv/x/blob/df0e6d2b54fffdff5ee716f1fa171381cfdeddf6/react/examples/todoapp/todo_app.go#L32-L48, correct?

I think the comment on that method gives you the answer you're after:

// Equals must be defined because struct val instances of TodoAppState cannot
// be compared. It is generally bad practice to have mutable values in state in
// this way; myitcv.io/immutable seeks to help address this problem.
// See myitcv.io/react/examples/immtodoapp for an example

TodoAppState is defined as:

type TodoAppState struct {
    items    []string
    currItem string
}

This makes values of type TodoAppState not comparable. Hence, via a .Equal method, you need to tell myitcv.io/react how to compare state values.

As the comment suggests however, it is generally bad practice to have mutable state like this.

Instead, I've preferred the pattern of immutable state, a la https://github.com/myitcv/x/blob/master/react/examples/immtodoapp/todo_app.go#L25-L28

Closing because I think I've covered everything here, but again, please shout if not.