React Input Element : Value vs Default Value React Input Element : Value vs Default Value reactjs reactjs

React Input Element : Value vs Default Value


The reason your input doesn't work is because you need to define the onChange function which actually sets the state with the updated value. You can probably do it inline since it only needs on statement like

<input type="text" value={this.state.inputVal} onChange={(e) => {this.setState({inputVal: e.target.value})}} />

However I would recommend you to use an onChange method as you can handle multiple inputs together with it and it looks cleaner

class EditForm extends React.Component {    constructor() {        super();        this.state = {                }    }    onChange(e) {         this.setState({[e.target.name]: e.target.value})    }    editTransaction(event) {        var transaction = this.props.transaction;        event.preventDefault();        var NewTransaction = {            transactions_data: {                amount: this.refs.amount.value            }        }        this.props.editTransaction(NewTransaction, transaction.id);    }    closeForm() {        this.props.closeForm();    }    render() {               return (            <div>                <br/>                <h4>Edit Transaction</h4>                <div className="btn btn-danger pull-right" onClick={this.closeForm.bind(this)}>close</div>                <div className="clearfix"></div>                <form onSubmit={this.editTransaction.bind(this)}>                    <div>                        <label for="amount">Amount</label>                        <input value={this.state.amount} onChange={(value) => this.onChange(value)} className="form-control"                               id="amount" name="amount" type="number"                               ref="amount"/>                         <input value={this.state.amount1} onChange={(value) => this.onChange(value)} className="form-control"                               id="amount1" name="amount1" type="number"                               ref="amount"/>                    </div>                    <br/>                    <br/>                    <input className="btn btn-info" type="submit" value="submit"/>                </form>            </div>        );    }}ReactDOM.render(<EditForm/>, document.getElementById('app'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react.min.js"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.0.2/react-dom.min.js"></script><div id="app"></div>