Skip to content

Input caret will jump to end of string when using store #178

@AlexThurston

Description

@AlexThurston

Using the basic-flow example and adding a name to the state:

const initialState: State = {
  count: 0,
  name: 'john doe',
};

And an action to set the name:

const actions = {
  increment:
    (): Action<State> =>
    ({ setState, getState }) => {
      setState({
        count: getState().count + 1,
      });
    },
  setName:
    (name: string): Action<State> =>
    ({ setState }) => {
      setState({
        name: name,
      });
    },
};

And a simple input component to the CounterHook component:

const CounterHook = () => {
  const [{ count, name }, { increment, setName }] = useCounter();

  return (
    <div>
      <h3>With Hooks</h3>
      <p>{count}</p>
      <p>{name}</p>
      <button onClick={increment}>+1</button>
      <input value={name} onChange={e => setName(e.target.value)} />
    </div>
  );
};

If text is added to the beginning, or middle of the name field, the first character will be correct, but the caret will jump to the end of the text after a single entry.

This is not the behaviour if I make a local state using React.useState

Here is a video of the behaviour:

Screen.Recording.2022-09-22.at.12.38.05.PM.mov

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions