Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
AlexThurston opened this issue Sep 22, 2022 · 5 comments
Open

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

AlexThurston opened this issue Sep 22, 2022 · 5 comments
Labels
bug Something isn't working

Comments

@AlexThurston
Copy link

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
@AlexThurston
Copy link
Author

You can also see this behaviour with the advanced-scoped-flow in it's stock form while using the input component.

@albertogasparin
Copy link
Collaborator

Ha, good catch, I've never known of this gotcha. It's related to facebook/react#955
React tracks the cursor position only for sync re-renders 😞 so, since we moved RSS updates to be scheduled by default (even if with a duration of just one tick), it loses the cursor position.
Quite tricky. Not sure how to handle this 🤔

@albertogasparin albertogasparin added the bug Something isn't working label Sep 23, 2022
@albertogasparin
Copy link
Collaborator

albertogasparin commented Oct 21, 2022

What do you think about a flushSync API (similar to the react-dom v18 util) to force updates to be dispatched synchronously?

const actions = {
  setName:
    (name: string): Action<State> =>
    ({ setState }) => {
      flushSync(() => {
        setState({ name: name });
      });
    },
};

Or an alternative could be adding a second, optional argument to setState:

const actions = {
  setName:
    (name: string): Action<State> =>
    ({ setState }) => {
      setState({ name: name }, { sync: true });
    },
};

@bholloway
Copy link

So for reference we are talking about flushSync in react.

Given the setState interface is already analogous to react then it's important to keep using react paradigms as much as possible. To avoid confusion and to take advantage of existing familiarity with react.

Having a function scope where the synchronous event occurs is useful in that arbitrary other operations can be located there be known to be synchronous.

In comparison the additional arguments on the setState() is less elegant IMO. Fails interface segregation principle adding a bunch more "options" to achieve what can be done by composition.

@AjithKumarvm
Copy link

Is there anyone fixing this bug? v2.7.2 is completely unusable because of this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants