If the input of a field must be equal to a global state, I usually do something like this (here in dexie.js, but I have similar issues with redux as well):
export const Friend = memo(({id}) => {
const friend = useLiveQuery(() =>
db.friends.get(id)
);
const deleteFriend = useCallback(() => db.friends.delete(id), [id]);
const changeName = useCallback((e) => db.friends.update(id, {name: e.target.value}), [id]);
return <>Name: {friend?.name}, Age: {friend?.age} <input value={friend?.name} onChange={changeName} /> <button onClick={deleteFriend}>Delete</button></>
})
The problem is that with this:
- I can't type accents that need two key presses (dead key), like ï
- even more annoying, I can't type something in the middle of the word, as the cursor is automatically pushed at the very end:
Here is an example where I moved my cursor to Th*is is a test
(*
being the position of the cursor) and typed ab ï
:
This seems to be related to the fact that react does not handle async updates… but then what would be the recommended way to solve this issue?
MWE
You can find a simple working example here https://github.com/tobiasBora/bug-dexie.