javascript - React Input don't change when I set a initial value - Stack Overflow

I am creating CRUD using ReactJS but I have a problem with the React Forms.in some cases, I need to se

I am creating CRUD using ReactJS but I have a problem with the React Forms.

in some cases, I need to set an initial value in my input and I am using a logical operator for this, everything is ok, but when I change the value from the input, the value from the input doesn't change.

My ponent:

export function Modal(props) {

  const [inputsValues, setInputsValues] = useState({
    id: '',
    nameInput: '',
    emailInput: '',
    phoneInput: ''
  })

  const handleInputChange = (event) => {
    console.log(event.target.value)
    inputsValues[event.target.name] = event.target.value
    setInputsValues()
  }
  const handleFormSubmit = (event) => {
    event.preventDefault()
    console.log(inputsValues)
  }

  return (
    <div className={`area-modal ${props.isOpen ? 'open' : null}`}>
      <div className="modal">
        <h1>{props.title}</h1>
        <form onSubmit={handleFormSubmit} action="">
          <label htmlFor="">Name</label>
          <div className="area-input">
            <input
              type="text"
              name="nameInput"
              value={inputsValues.nameInput}
              onChange={handleInputChange}
            />
          </div>
          <label htmlFor="">Phone</label>
          <div className="area-input">
            <input
              type="text"
              name="phoneInput"
              value={props.dataForm ? props.dataForm.phone : ''}
              onChange={handleInputChange}
            />
          </div>
          <label htmlFor="">Email</label>
          <div className="area-input">
            <input
              type="email"
              name="emailInput"
              value={props.dataForm ? props.dataForm.email : ''}
              onChange={handleInputChange}
            />
          </div>
          <button>{props.buttonText}</button>
        </form>
      </div>
    </div>
  )
}

I am creating CRUD using ReactJS but I have a problem with the React Forms.

in some cases, I need to set an initial value in my input and I am using a logical operator for this, everything is ok, but when I change the value from the input, the value from the input doesn't change.

My ponent:

export function Modal(props) {

  const [inputsValues, setInputsValues] = useState({
    id: '',
    nameInput: '',
    emailInput: '',
    phoneInput: ''
  })

  const handleInputChange = (event) => {
    console.log(event.target.value)
    inputsValues[event.target.name] = event.target.value
    setInputsValues()
  }
  const handleFormSubmit = (event) => {
    event.preventDefault()
    console.log(inputsValues)
  }

  return (
    <div className={`area-modal ${props.isOpen ? 'open' : null}`}>
      <div className="modal">
        <h1>{props.title}</h1>
        <form onSubmit={handleFormSubmit} action="">
          <label htmlFor="">Name</label>
          <div className="area-input">
            <input
              type="text"
              name="nameInput"
              value={inputsValues.nameInput}
              onChange={handleInputChange}
            />
          </div>
          <label htmlFor="">Phone</label>
          <div className="area-input">
            <input
              type="text"
              name="phoneInput"
              value={props.dataForm ? props.dataForm.phone : ''}
              onChange={handleInputChange}
            />
          </div>
          <label htmlFor="">Email</label>
          <div className="area-input">
            <input
              type="email"
              name="emailInput"
              value={props.dataForm ? props.dataForm.email : ''}
              onChange={handleInputChange}
            />
          </div>
          <button>{props.buttonText}</button>
        </form>
      </div>
    </div>
  )
}
Share Improve this question edited Jul 20, 2021 at 6:58 Sanira Liyanage 1,2473 gold badges15 silver badges32 bronze badges asked Jul 20, 2021 at 2:12 PedrohhcunhaPedrohhcunha 512 silver badges6 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Instead of

inputsValues[event.target.name] = event.target.value;
setInputsValues();

you need to use setInputsValues to update the state with the new value:

const { name, value} = e.target;
setInputsValues({ ...inputsValues, [name]: value });

You need to pass the updated input data into your setInputsValues, for example:

  const handleInputChange = (event) => {
    console.log(event.target.value)
    inputsValues[event.target.name] = event.target.value
    setInputsValues({...inputsValues});
  }

When working with object-based states like this, it is usually good practice to use the destructured assignment to ensure the state updates.

When you're using reacts useState, you don't need to set the state manually. It handles state for you. Also it's better to use object spread to change the state. So you can change handleInputChange same the bellow:

  const handleInputChange = (event) => {
    const name = event.target.name;
    const value = event.target.value;
    setInputsValues({...inputsValues, [name] : value })
  }

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744965659a4603664.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信