Skip to content

deprecate define_state (?) #89

Open
@catmando

Description

@catmando

now that implicit state construction has been (tentatively) added, should we deprecate define_state?

I believe so, why? Because it makes the connection between "state variables" and "instance variables" clear. You don't explicitly declare instance variables, so why should you declare state variables?

Good style practice then would be to initialize all state variables (like you would instance variables) in the before_mount macro.

More important however is how state variables get initialized. The current define_state macro allows initializing but its very confusing, as the initial value is computed once per class, NOT per instance as you would expect. That is why I believe that states should be "created" within the before_mount macro, because that allows the state to be computed on a per instance basis (just like normal instance variables)

consider this code:

class MyComponent < React::Component::Base
  param :users, type: [User]
  before_mount do
    state.users! params.users
    state.mount_time! Time.now
  end
  ...

if good style insists that all state variables are initialized in the block (as above)
then there is no redundancy.

On the other hand if we determine that good style uses define_state, then you end up with redundant code.

Moreover the above makes its much clearer how and when the state is initialized.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions