1

I have a problem with my React project, I have an input element like below but I can't edit the text of the input. I just can edit the input text only when I delete the value attribute, but I want a default value for it.

  <div className="form-group">
       <label>Email:</label> 
       <input  
          className="form-input" 
          type="text" 
          value="[email protected]" 
          name="email">
       </input>
  </div>
3
  • you want to change tu value or want to add placeholder?? Commented Mar 17, 2020 at 9:29
  • 1
    I'd recommend reading reactjs.org/docs/forms.html Commented Mar 17, 2020 at 9:29
  • 1
    This is the first thing that you need to know about react. Please go over the tutorial: reactjs.org/docs/forms.html Commented Mar 17, 2020 at 9:31

4 Answers 4

4

If you've an uncontrolled component, you might want to use the defaultValue property instead of value (See this reference)

Sign up to request clarification or add additional context in comments.

Comments

0

You can use useRef or defaultValue

import React, { useRef, useEffect } from "react";

const input = useRef();
useEffect(() => {
    input.current.value = "[email protected]";
}, []);

<input ref={input} className="form-input" type="text" name="email" />`

Comments

0

Here is a sample code how to use inputs in react

class NameForm extends React.Component {
  constructor(props) {
    super(props);
    this.state = {value: ''};

    this.handleChange = this.handleChange.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
  }

  handleChange(event) {
    this.setState({value: event.target.value});
  }

  handleSubmit(event) {
    alert('A name was submitted: ' + this.state.value);
    event.preventDefault();
  }

  render() {
    return (
      <form onSubmit={this.handleSubmit}>
        <label>
          Name:
          <input type="text" value={this.state.value} onChange={this.handleChange} />
        </label>
        <input type="submit" value="Submit" />
      </form>
    );
  }
}

Comments

0

Set the default value inside the state object and attach a change handler to the input to capture the changes:

Sample codesandbox: https://codesandbox.io/s/react-basic-class-component-22fl7

class Demo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
                    inputValue: '[email protected]'
                 };
  }

  handleChange = event => {
      this.setState({ inputValue: event.target.value }, () =>
          console.log(this.state.inputValue)
      );
  };

  render() {
    return (
       <div className="form-group">
         <label>Email:</label> 
         <input  
           className="form-input" 
           type="text" 
           value={this.state.inputValue}
           onChange={this.handleChange} 
           name="email">
         </input>
       </div>
    );
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.