Altering react form but input value not editable

僤鯓⒐⒋嵵緔 提交于 2019-12-25 06:51:51

问题


I came from angularjs background, with 2 way binding I don't have to worry much, just declare a submit function, I can get the changed or unchanged value.

I'm stuck with react now. My input is not editable.

class HelloWorldComponent extends React.Component {

  constructor(){
    super();

    this.handleChange = this.handleChange.bind(this)
  }

  render() {
    const person = {"name":"james"};
    return (      
      <input onChange={this.handleChange} type="text" placeholder="name" value={person.name} />      
    );
  }

  handleChange(e){

  }
}

React.render(
  <HelloWorldComponent />, document.getElementById('react_example')
);

http://jsbin.com/huluqifanu/1/edit?js,console,output

Also what to do next? should I set the api data to a state first?


回答1:


If you do not want to use Controlled Components then you can use Uncontrolled Components.

Specifically, you can use the defaultValue prop instead of value on your input.

As to your second question, you will have to be more clear what you are asking or perhaps better to ask in a separate Q altogether.



来源:https://stackoverflow.com/questions/42639577/altering-react-form-but-input-value-not-editable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!