Coder Social home page Coder Social logo

Comments (6)

hon2a avatar hon2a commented on May 6, 2024

Reward for tracing it to the source was at least finding a work-around, which is to pass in a constant.

const defaultValue = []
const form = () => (
  <SimpleForm>
    <SelectArrayInput defaultValue={defaultValue} />
  </SimpleForm>
)

from admin-on-rest.

fzaninotto avatar fzaninotto commented on May 6, 2024

That's a common React gotcha: you're passing a new empty array object each time the form rerenders, and specifying a default value rerenders the form. You have to define the default value as a constant (as we've explained for the validators, too).

from admin-on-rest.

hon2a avatar hon2a commented on May 6, 2024

I'm aware the code is creating a new Array instance on each render. The fact that it creates infinite update loop is clearly not a "React gotcha", but a bug in how defaultValue is being handled - re-rendering the whole form on defaultValue change has nothing to do with React, it's your choice and it's not obvious in the least. (And if I needed the defaultValue to actually change, I'd have to do some shenanigans like keeping it in an instance property to work around this behaviour.)

I'd say this warrants a clear warning in the docs at the very least. Additionally, the setup seemed to work not too far back, so maybe this changed with some dependency update.

Even more interesting (but unrelated) is how a function in defaultValue is handled. Accepting a function without arguments only makes sense if it's not expected to be pure. Then resolving it only when the function changes doesn't seem to make much sense.

from admin-on-rest.

hon2a avatar hon2a commented on May 6, 2024

Btw. the docs actually contain code samples with the same issue (see defaultValue={new Date()} and defaultValue={() => uuid()}), which illustrates my point pretty well.

from admin-on-rest.

fzaninotto avatar fzaninotto commented on May 6, 2024

We're relying on redux-form for handling default values. I believe the problem lies there.

from admin-on-rest.

hon2a avatar hon2a commented on May 6, 2024

Nope, you're in control of dispatching its INITIALIZE action. redux-form only reacts by updating the form but doesn't control how this affects the place where you dispatch the action from. (It has no insight into the structure of the app using it.)

from admin-on-rest.

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.