onChangeイベントハンドラでは、userTxtに対してsetState()を呼び出すだけですが、カラーオブジェクトの状態も設定されているように見えます。私にとって奇妙なことは、これは色オブジェクトに対してのみ起こりますが、年齢変数には起こらないということです。SetStateがハンドラ内の他のオブジェクトの更新に反応しているのはなぜですか?
誰かがなぜこれが起こっているのか説明できますか? Here is a link to my webpackbin example. As you write in the input, the state is changed on the color object.。
私は、誰かがなぜこれが起こっているのかのメカニズムを私に説明してくれることを願っていました。事前にどうもありがとうございました。
import React, { Component } from 'react';
export default class Hello extends Component {
constructor() {
super();
this.handleMe = this.handleMe.bind(this);
this.state = {
age: 21,
colors: {
best: 'blue',
second: 'green'
},
userTxt: ""
}
}
handleMe(e) {
let myAge = this.state.age;
let myColors = this.state.colors;
myAge = myAge + 1;
myColors['best'] = 'mistyrose';
myColors['second'] = 'red';
this.setState({ userTxt: e.target.value });
}
render() {
const { age, colors, userTxt} = this.state;
return (
<div>
<form action="">
<input type="text"onChange={this.handleMe}/>
<div>Age: {age}</div>
<div>Colors - best: {colors.best}</div>
<div>Colors - second: {colors.second}</div>
<div>UserTxt: {userTxt}</div>
</form>
</div>
)
}
}[enter link description here][1]
[1]: https://www.webpackbin.com/bins/-KvFx-s7PpQMxLH0kg7m