What are the differences between defaultValue and value in select?

痴心易碎 提交于 2019-12-25 18:59:17

问题


I have two react components, parent and child. I'm passing a prop to a child component, and I want to use that prop to set the defaultValue on a select input. However, if that property changes, I'd like for the select default value to change as well.

When I set default value in select, I can choose one of the options that is a part of that selector. If I use value instead, the 'default' changes as the property updates, but I can't select any of the options.

class Selector extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
       <select defaultValue={this.props.value}>
         <option>1</option>
         <option>2</option>
       </select>
    )
  }
}

I'd like for the value to change, and I realize that it is not re rendering even though the prop has changed. I'm looking for a work around.


回答1:


When you don't have onChange handler you need to put your value as defaultValue, but in value when you have onChange handler.

You can do this,

class Selector extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      selected: props.value
    }
  }
  change = (event) =>{
     this.setState({selected: event.target.value});
  }
  componentDidUpdate(prevProps, prevState) {
    if (prevState.selected !== this.props.value) {
      this.setState({selected: this.props.value})
    }
  }
  render() {
    return (
       <select value={this.state.selected} onChange={this.change}>
         <option>1</option>
         <option>2</option>
       </select>
    )
  }
}



回答2:


defaultValue is selected value while very first time loading and value is selected value every time to change option value




回答3:


I'm quoting:

The difference between the defaultValue and value property, is that defaultValue contains the default value, while value contains the current value after some changes have been made. If there are no changes, defaultValue and value is the same.

The defaultValue property is useful when you want to find out whether the contents of a text field have been changed.

What that actually means is that if you put defaultValue, this value will be initialized to the input and that's it, you can change value and the text will change.

But if you put value, you would need to change that value given to the input in the first place in order for input text to change.

Look at this example, all using the same state, but behaving differently.

// Example class component
class Thingy extends React.Component {
  
  constructor(props) {
    super(props);
    this.state = { value: 'test' }
  }
  
  onChange(e) {
    this.setState({ value: e.target.value });
  }
  
  render() {
    return (
      <div>
        <div><b>default value</b> (you can edit without changing this.state.value)</div>
        <input defaultValue={this.state.value}></input>
        
        <div><b>value</b> (you can't edit because it does not change this.state.value)</div>
        <input value={this.state.value}></input>

        <div><b>value</b> (you can edit because it has onChange method attached that changes this.state.value) <br /> <b>NOTE:</b> this will also change second input since it has attached the same state with <b>value</b> property, but won't change first input becase same state was attached as <b>defaultValue</b></div>
        <input value={this.state.value} onChange={e => this.onChange(e)}></input>
      </div>
    );
  }
}

// Render it
ReactDOM.render(
  <Thingy />,
  document.body
);
div > div {
  font-size: 16px;
}

input + div {
  margin-top: 20px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>


来源:https://stackoverflow.com/questions/56988752/what-are-the-differences-between-defaultvalue-and-value-in-select

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