Giter Site home page Giter Site logo

Comments (3)

Gelio avatar Gelio commented on May 21, 2024 1

I am not convinced that this is a responsibility of the linter to report such behavior 🙂 Statically analyzing the code would not result in enough information to determine that setCounterRender will cause an endless loop.

Moreover, in some cases, the code snippet you provided is the wanted behavior, as in some cases shouldRender may be falsy. That is why it would not be easy to stop such behavior even during runtime.

All in all, I have no easy idea on how to implement such verification, therefore I am closing this issue. If you, or anyone else, has an implementation idea, feel free to create a PR 🙂

from tslint-react-hooks.

Gelio avatar Gelio commented on May 21, 2024

Thanks for posting an issue 🙂

Are you sure that useState inside useEffect does not result in any violations? For me, the following snippet:

function MyComponent() {
  React.useEffect(() => {
    React.useState(() => {

    })
  })
}

results in a rightful rule violation:

image

Could you share a snippet of this case that does not result in a rule violation? 🙂

from tslint-react-hooks.

linonetwo avatar linonetwo commented on May 21, 2024

Hi, I just found that, in some case likes:

  const [currentRenderCounter, setRenderCounter] = useState(0);
  useEffect(() => {
    if (shouldRender) {
      setRenderCounter(counter => counter + 1);
      console.log(currentRenderCounter);
...

Will cause infinite loop of rendering.

But in some case, it's fine to use state in useEffect... I'm still confise why some times it works.

The safe way is to useRef:

  const renderCounter = useRef(0);

  useEffect(() => {
    if (shouldRender) {
      renderCounter.current += 1;
      console.log(renderCounter.current);
...

from tslint-react-hooks.

Related Issues (18)

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.