-
Notifications
You must be signed in to change notification settings - Fork 17
Gotchas
PAGE HAS MOVED TO https://github.com/myitcv/x/blob/master/react/_doc/gotchas.md
setState
in ReactJS is defined as follows:
void setState(
function|object nextState,
[function callback]
)
Performs a shallow merge of nextState into current state...
setState()
does not immediately mutatethis.state
but creates a pending state transition. Accessingthis.state
after calling this method can potentially return the existing value.
There are two significant differences to these semantics in myitcv.io/react
:
-
SetState(...)
immediately reflects viaState()
- There is no shallow merge as state is a struct value. Hence to only partially update state, you need only mutate a copy of the current value:
type MyAppDef struct {
ComponentDef
}
type MyAppState struct {
name string
age int
}
// ...
func (p *MyAppDef) onNameChange(se *SyntheticEvent) {
target := se.Target().(*dom.HTMLInputElement)
ns := p.State()
ns.name = target.Value
p.SetState(ns)
}
Both state and props are defined as struct types and struct values are used for current state or props. Hence we can generally rely on comparison between new and old state/props values to determine whether or not a component should re-Render
.
In case either state or props struct types are defined with slice, map, and function fields (this is incidentally not advised, wiki on immutable values to follow), comparison between struct values cannot be used (a simple example of this). In this case you can define an Equals
method:
func (c TodoAppState) Equals(v TodoAppState) bool {
// ...
}
React 16 introduced a change in the way that unknown DOM attributes are handled. Taking the example from the linked React article, if previously (i.e. React 15 and earlier) you wrote JSX with an attribute that React doesn’t recognise, React would just skip it. For example, this:
// Your code:
<div mycustomattribute="something" />
would render an empty div to the DOM with React 15:
// React 15 output:
<div />
In React 16 unknown attributes will end up in the DOM:
// React 16 output:
<div mycustomattribute="something" />
myitcv.io/react
does NOT support this, ror the obvious reason that the props of the DOM elements are predefined (and hence as the caller you can't provided ad hoc, custom attributes else it would be a compile error).
Conceivably we could support this via a catch-all, map[string]interface{}
-typed attribute on all DOM element prop types. But this feels like a bit of a back door and one that right now doesn't have anyone knocking at it. That is to say, we'll wait until this becomes a massively pressing need before changing the status quo.
As of commit b382bf4
myitcv.io/react
bundles React 16.
For React 15, please use the react_15
branch.