本文介绍了更改反应形式但输入值不可编辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自angularjs背景,使用2种方式绑定,我不必担心,只需声明一个Submit函数,就可以获取更改或未更改的值.

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 ,控制台,输出

接下来该怎么办?我应该先将api数据设置为状态吗?

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

推荐答案

如果您不想使用受控组件,则可以使用不受控制的组件.

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

具体来说,您可以在input上使用defaultValue道具代替value.

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.

这篇关于更改反应形式但输入值不可编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:05
查看更多