0

I declared my state like below

const [updatedStep, updateStepObj] = useState(
    panel === 'add'
        ? new Step()
        : {
                ...selectedStep
          }
);

and I have elements like

        <TextField
            label="Title"
            value={updatedStep.title}
            onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
                updateStepObj({ ...updatedStep, title: e.currentTarget.value })
            }
        />

I am getting Typescript error for onChange event. How can I get rid of error?

enter image description here

1 Answer 1

1

You must put the same type of data for the onChange method as that TextField is declared, so it would be like this:

<TextField
   label="Title"
   value={updatedStep.title}
   onChange={(e: React.FormEvent<HTMLInputElement>, newValue?: string) =>
       updateStepObj({ ...updatedStep, title: e.currentTarget.value })
   }
/>

Note: to forget about this mess, the best thing to do before adding your method to the event is to hover over it with the mouse and it will tell you the tipado. Then you write the same and perform the action you want.

Anyway if you want something more exact I add a demo in CodeSandbox and I'll take a look :)

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.