I am trying to set a boolean value using a setEditMode
function but unfortunately, I am encountering with a type error while invoking the component in index.tsx file.
The error is Type 'boolean' is not assignable to type '(editMode: boolean) => void'.ts(2322)
Here is the Sandbox link
The error can be checked in the index.tsx
file.
Can anybody guide me with the correct path Why is it so and what is the best way to solve it.
2
Answers
As the comments mentioned you are passing a boolean to a prop that expects a function.
You can avoid that by passing the editMode value instead like so.
And then for what I believe is the functionality you want you can modify the App.js to be like this
This would use the initial edit value from your index file to create a new state. And then clicking on the edit button would update the editMode state to true
I would avoid props in the
App
component. Instead, I would create anEditableComponent
that takes these props. Just keep track of the state inApp
or a context/store. The internal component can handle local updates to the value.The following component takes an edit mode and a value. The component itself updates the local value, and upon saving, will update the value in the
App
. I wrapped the<input>
in a form so that he Enter key will trigger the update (if you do not wish to click the button).I also added a cancel action that reverts the value back to the original.
Snippet
Here is a working snippet. Note that it’s JSX rather than TSX, so types will not be defined, but the principle business logic remains.