Coder Social home page Coder Social logo

Comments (8)

alisherks avatar alisherks commented on October 16, 2024 1

Is there any available workaround for this? This limitation also affects the useImperativeHandle.

from react.

jeremy-code avatar jeremy-code commented on October 16, 2024

Related: #30716

from react.

parthnegi21 avatar parthnegi21 commented on October 16, 2024

Try this

import { useRef, useState, useEffect } from "react";

const UNINITIALIZED = Symbol("UNINITIALIZED");

export const useOnce = <T,>(initialValue: T | (() => T)) => {
const ref = useRef < T | typeof UNINITIALIZED > (UNINITIALIZED);
const [value, setValue] = useState < T | typeof UNINITIALIZED > (UNINITIALIZED);

useEffect(() => {
if (ref.current === UNINITIALIZED) {
const resolvedValue = typeof initialValue === "function" ? initialValue() : initialValue;
ref.current = resolvedValue;
setValue(resolvedValue);
} else {
setValue(ref.current);
}
}, [initialValue]);

return value;
};

export default function MyApp() {
return

Hello World
;
}

State Initialization: Added a state value to store the initialized value.

useEffect: The ref is now initialized inside the useEffect hook, which sets the value both in the ref and in the state.

Return Value: The hook returns the state value, ensuring that the initial render doesn't access ref.current

from react.

josephsavona avatar josephsavona commented on October 16, 2024

Thanks for posting. This is a known limitation of the new linter.

from react.

hlege avatar hlege commented on October 16, 2024

i also run into the problem in a different scenario:
when passing the ref object to a custom hook the compiler is not optimizing my code. if i remove the ref access it works as expected.

// pass the ref object to a custom hook and it is preventing the optimizations.  
const mergedRefs = useMergedRef<HTMLDivElement>(ref, setPopperElement);

from react.

iahu avatar iahu commented on October 16, 2024

Do you deprecated to use useRef? if not, how to use it?

const panelRef = useRef<HTMLDivElement>(null);
const width = panelRef.current?.clientWidth ?? 256; // eslint error here

from react.

alisherks avatar alisherks commented on October 16, 2024

To anyone encountering optimization issues with the usage of useImperativeHandle, you can place the forwardedRef inside a useState hook.

const [refs] = useState(() => {
    return { forwardedRef };
});

This works against latest experimental release (0.0.0-experimental-4e0eccf-20240830). It probably breaks the rules, but so far didn't encounter serious issues.

from react.

nkalpakis21 avatar nkalpakis21 commented on October 16, 2024

built an app to get paid for this PR
https://www.n0va-io.com/discover/facebook/react

from react.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.