javascript - MobX: Update value in input field - Stack Overflow

I use a simple input field which should be controlled by an @observable value:<div><input valu

I use a simple input field which should be controlled by an @observable value:

<div>
    <input value={this.props.appState.myValue} onChange={this.handleChange}/>
    value: {this.props.appState.myValue}
</div>

After updating myValue to undefined the displayed value changes, but the value in the input field keeps the same value.

Initial state:

[initial]
value: initial

After update:

[initial]
value:

I use a simple input field which should be controlled by an @observable value:

<div>
    <input value={this.props.appState.myValue} onChange={this.handleChange}/>
    value: {this.props.appState.myValue}
</div>

After updating myValue to undefined the displayed value changes, but the value in the input field keeps the same value.

Initial state:

[initial]
value: initial

After update:

[initial]
value:
Share Improve this question edited Dec 27, 2016 at 15:25 Tholle 113k22 gold badges208 silver badges197 bronze badges asked Dec 27, 2016 at 14:58 Matthias MMatthias M 15k18 gold badges98 silver badges129 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

I would suspect you are not making your ponent into an observer. Try this example:

@observer
class App extends React.Component {
  handleChange = (e) => {
    this.props.appState.myValue = e.target.value; 
  };
  render() {
    const { appState } = this.props;
    return (
      <div>
        <input value={appState.myValue} onChange={this.handleChange} />
        value: {appState.myValue}
      </div>
    );
  }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745350301a4623792.html

相关推荐

  • javascript - MobX: Update value in input field - Stack Overflow

    I use a simple input field which should be controlled by an @observable value:<div><input valu

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信