question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

When a TextInput is inside a Portal, cursor jumps around on edit

See original GitHub issue

Here is a code sandbox demonstrating the issue

Position your cursor at the first character of the TextInput and type a several times

Note that while the first appeared at the correct location, afterwards the cursor jumps to the end and you end up with something like afooaa. My guess is that the input is (incorrectly) re-mounting.

I’m using this very simple component

import React, { useState } from "react";
import { Portal, TextInput, Provider } from "react-native-paper";

export default () => {
  const [data, setData] = useState(`foo`);

  return (
    <Provider>
      <Portal>
        <TextInput value={data} onChangeText={setData} />
      </Portal>
    </Provider>
  );
};

When not inside of a Portal this works fine.

It also sometimes works fine even in a Portal, I have a team member that tried to fix this issue by modifying our code, and actually got it working from inside a Portal by introducing a ton of duplication and awkward state. I have not been able to narrow down what exactly makes it work again, but this reduced test case shows the incorrect behavior regardless.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:8

github_iconTop GitHub Comments

1reaction
zombie6888commented, Jan 18, 2021

I guess, if you move Portal outside component, somewhere to parents, it will work. Try out my workaround from here: https://github.com/jeremybarbet/react-native-portalize/issues/6#issuecomment-761907191

1reaction
togakangaroocommented, Feb 13, 2020

Using this terrible hack instead of TextInput is terrible but seems to work

type TextInputInAPortalProps = TextInputProps & {
  value: string
  onChangeText: (t: string) => void

}

// Use this whenever you need a TextInput inside that appears inside of a Portal
//
// Wait what now?
// This seems to fix this bug: https://github.com/callstack/react-native-paper/issues/1668
export const TextInputInAPortal = (props: TextInputInAPortalProps) => {
  const [value, setValue] = useState(props.value)

  useEffect(() => {
    if(value !== props.value)
      setValue(props.value)
  }, [props.value])

  const onChangeText = (text: string) => {
    setValue(text)
    props.onChangeText(text)
  }
  return React.createElement(TextInput, {...props, value, onChangeText})
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cursor jumps to the end while typing in a text input box
Cursor jumps to the end while typing in a text input box · Select the form · Click Edit fields · Check the...
Read more >
React controlled input cursor jumps - Stack Overflow
If your value is controlled by state, React will maintain the input's cursor position. The problem is when the input receives a change...
Read more >
The Curious Case of Cursor Jumping - Mutually Human
Cursor jumping is simply when the cursor jumps from one location to another, unexpectedly, while the user is typing into a text field....
Read more >
TextInput - React Native
TextInput. A foundational component for inputting text into the app via a keyboard. Props provide configurability for several features, ...
Read more >
Fix Mouse Cursor jumping around in Windows 10 / 11
Solution 5] Try to change the Mouse pointer size & color · Case 1 – For Windows 10 · Case 2 – For...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found