Git Product home page Git Product logo

Comments (5)

shulcsm avatar shulcsm commented on May 25, 2024

I wonder if the whole thing should not be using useSyncExternalStore?

from react-final-form.

gertdreyer avatar gertdreyer commented on May 25, 2024

The SetFieldValueIfEmpty violates React functional component principles and possibly the rules of hooks.

For this kind of behaviour see subscribe.

from react-final-form.

sudkumar avatar sudkumar commented on May 25, 2024

Hi @gertdreyer. Thank you. I will checkout the subscribe API.

Can you please elaborate the violation of react functional components/hooks principles in the above example?

from react-final-form.

gertdreyer avatar gertdreyer commented on May 25, 2024

A functional component have to return a React Element . Similarly, a class-style component has to have a render method.

On the useEffect. Try playing with console.count(fieldname) in the useEffect to see the inefficiency. useEffect is actioned after the every render - you know before rendering that your field needs a default value. You also open yourself up to infinite render loops using setState in a useEffect if your condition is not well defined.

from react-final-form.

sudkumar avatar sudkumar commented on May 25, 2024
  1. It was a typo to not add a return null but that won't change the strict behaviour. You will still see the results shown in the screenshot.
  2. It should be safe to call setState or field.change inside useEffect with my conditions. The only (variable) dependency to the Effects the value.

Just wanted to clarify again that the issue is between the above functionality during React's Strict and Non-Strict Modes. In StrictMode, useEffect is executed twice during the first render causing the listeners to execute twice where as in non-StrictMode, listeners fail to correctly set-up.

from react-final-form.

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.