Skip to content

Commit

Permalink
fix: flushSync
Browse files Browse the repository at this point in the history
  • Loading branch information
BartoszGrajdek committed Mar 18, 2024
1 parent 9509076 commit e90a031
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/components/Composer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function Composer(
const [caretContent, setCaretContent] = useState('');
const [valueBeforeCaret, setValueBeforeCaret] = useState('');
const [textInputWidth, setTextInputWidth] = useState('');
const [isRendered, setIsRendered] = useState(false);
const isScrollBarVisible = useIsScrollBarVisible(textInput, value ?? '');

useEffect(() => {
Expand All @@ -125,7 +126,7 @@ function Composer(
const addCursorPositionToSelectionChange = (event: NativeSyntheticEvent<TextInputSelectionChangeEventData>) => {
const webEvent = event as BaseSyntheticEvent<TextInputSelectionChangeEventData>;

if (shouldCalculateCaretPosition && textInput.current) {
if (shouldCalculateCaretPosition && isRendered) {
// we do flushSync to make sure that the valueBeforeCaret is updated before we calculate the caret position to receive a proper position otherwise we will calculate position for the previous state
flushSync(() => {
setValueBeforeCaret(webEvent.target.value.slice(0, webEvent.nativeEvent.selection.start));
Expand Down Expand Up @@ -264,6 +265,7 @@ function Composer(
if (typeof ref === 'function') {
ref(textInput.current);
}
setIsRendered(true);

return () => {
if (isReportActionCompose) {
Expand Down

0 comments on commit e90a031

Please sign in to comment.