1

I am trying to edit user input but the click event point to the input at array 0. i wanted to be able to render only the input to be edited.

The full project is available on sandbox via the link below. I am new to react developing and struggling with state. Can someone help, please?

To see what i mean by the above, please enter some input and click add 2 different input then edit the car. Only the inputs 0 is rendered. https://codesandbox.io/s/eloquent-feather-gc88n?file=/src/header/header.js

Thank Leo

import React, { useState } from "react";

const EditSkillsForm = ({
  handleUpdateInput,
  inputs,
  handleCancelClick,
  //setCurrentSkill,
}) => {
  const [currentSkill, setCurrentSkill] = useState({});
  const [isEditing, setIsEditing] = useState(true);
  // const [ onSubmitEditing, SetOnSubmitEditing ] = useState("")

  function handleEditInputChange(e) {
    setCurrentSkill(e.target.value);
  }

  handleCancelClick = (e) => {
    setIsEditing(false);
    console.log("Cancel edit");
    //return {...prevState, isEditing : isEditing }
  };

  return (
    <>
      {isEditing ? (
        <div>
          <h4>Edit Skill</h4>
          <input
            className="mb-2"
            size="lg"
            onChange={handleEditInputChange}
            type="text"
            name="Update skill"
            placeholder="Update skill"
            value={[inputs[0]]}
          />
          <button className="btn btn-primary mx-2" onClick={handleUpdateInput}>
            Update
          </button>
          <button onClick={() => handleCancelClick()}>Cancel</button>
        </div>
      ) : null}
    </>
  );
};

export default EditSkillsForm;
2
  • Seems like inputs is coming from props, but you seem to want to edit it using the useState, why not handle it using the setCurrentSkill directly? What does inputs[0] contain and how is it handled in the parent component? Commented Oct 19, 2021 at 9:16
  • Hi @Icepickle , it was initially inputs but was not rendering what i wanted, the thing is i am still learning and quite new to react js, so i am in a try error method of getting it through. thanks for helping. You can have access to full project on the sandbox link Commented Oct 19, 2021 at 9:22

1 Answer 1

1

If you are editing 1 element at a time you shouldn't pass all the inputs. Only pass the object you wanna show/permit to edit.


const EditSkillsForm = ({ 
  handleUpdateInput, 
  input, 
  handleCancelClick, 
  //setCurrentSkill,
   
  })

...

<input
      className="mb-2"
      size="lg"
      onChange={handleEditInputChange}
      type="text"
      name="Update skill"
      placeholder="Update skill"
      value={input}
/>
Sign up to request clarification or add additional context in comments.

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.