小编典典

defaultValue更改不会重新呈现输入

reactjs

我不知道为什么这不起作用

演示版

我有以下es6代码

const {createFactory, createClass, DOM: { label, input, button }} = React;

const tester = createFactory(createClass({
  render() {
      return label({}
               ,`Name: ${this.props.name}`
               ,input({defaultValue: this.props.name})
               ,button({onClick: this.changeName}, "Change")
             )
  },
  changeName() {
    this.setProps({name: "Wilma"})
  }
}) )

React.render(tester({name: "Fred"}), document.querySelector('body'))

单击按钮可以清楚地更改道具,但是旧defaultValue的仍然在输入中!那有什么呢?我究竟做错了什么?这是一个错误吗?有解决方法吗?


阅读 509

收藏
2020-07-22

共1个答案

小编典典

您仅指定其默认值,但不告诉它通过更改props来更改其值。

,input({value: this.props.name})

当this.props.name更改时,将更改该值。

http://output.jsbin.com/melitecimo

2020-07-22