I am new to React and REDUX, I just developed a simple responsive application, and now I encounter a problem when I render an input element inside my component, if I set the element to βvalueβ, it becomes read-only, but if I install value on defaultValue, it will never be updated again when I re-update my state.
Here is my code:
import React from "react"; export default class EditForm extends React.Component { 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() { var {amount}=this.props.transaction; 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={amount} onChange={(value) => this.onChange(value)} className="form-control" id="amount" name="amount" type="number" ref="amount"/> </div> <br/> <br/> <input className="btn btn-info" type="submit" value="submit"/> </form> </div> ); } }
and then I found out if I can make a mistake from this by adding onChange={(value) => this.onChange(value)} on my input element, it works correctly (it updates when updating the props or status, and I can reuse enter value), but I think this is not the right solution, because it causes errors on my browser console, This is because the this.onChange function does not exist.
please kindly help me solve this problem and thanks in advance
Hello,
Vidy
source share