React - change input defaultValue by passing props

后端 未结 7 1956
星月不相逢
星月不相逢 2020-12-24 13:02

Consider this example:

var Field = React.createClass({
    render: function () {
        // never renders new value...
        return (
            

        
相关标签:
7条回答
  • 2020-12-24 13:59

    Use conditional rendering, then the component will load correct initial value. Something like in this module:

    class MenuHeaderInput extends React.Component{
        constructor(props){
            super(props);
            this.handleBlur = this.handleBlur.bind (this);
        }
        handleBlur (e) {
            this.props.menuHeaderUpdate(e.target.value);
        }
        render(){
            if (this.props.menuHeader) {
                return (
                    <div className="w3-row w3-margin" onClick = {() => this.props.handleTitleClick (10)}>
                        <div className="w3-third" ><pre></pre></div>
                        <input
                            className = {"w3-third w3-input w3-jumbo " + EDIT_COLOR}                
                            type = "text"
                            defaultValue = {this.props.menuHeader}
                            onBlur = {this.handleBlur}
                        />
                        <div className="w3-third" ><pre></pre></div>                
                    </div>
                )
            }
            else {
                return null;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题