Skip to content
This repository was archived by the owner on Oct 19, 2018. It is now read-only.
This repository was archived by the owner on Oct 19, 2018. It is now read-only.

need to make sure observer has not been deleted before doing a delayed update #6

Open
@catmando

Description

@catmando

SetState is generally delayed to the end of the rendering cycle, and all updates are sorted and queued to prevent multiple updates of the same value.

If component A gets a state update during this delayed update process, and that causes another component to unmount, and that component is scheduled to receive state updates, react.js generates a warning (because you will attempt to do a state update on an unmounted component.)

The fix is to simply check to see if the observer has been removed from the current_observers list.

module React
  class State

    def self.set_state(object, name, value, delay=ALWAYS_UPDATE_STATE_AFTER_RENDER)
      states[object][name] = value
      if delay || @bulk_update_flag
        @delayed_updates ||= Hash.new { |h, k| h[k] = {} }
        @delayed_updates[object][name] = [value, Set.new]
        @delayed_updater ||= after(0.001) do
          delayed_updates = @delayed_updates
          @delayed_updates = Hash.new { |h, k| h[k] = {} }
          @delayed_updater = nil
          updates = Hash.new { |hash, key| hash[key] = Array.new }
          delayed_updates.each do |object, name_hash|
            name_hash.each do |name, value_and_set|
              set_state2(object, name, value_and_set[0], updates, value_and_set[1])
            end
          end
          updates.each { |observer, args|
            observer.update_react_js_state(*args) if current_observers.key?(observer)
          }
          updates = nil
        end
      elsif @rendering_level == 0
        updates = Hash.new { |hash, key| hash[key] = Array.new }
        set_state2(object, name, value, updates)
        updates.each { |observer, args| observer.update_react_js_state(*args) }
      end
      value
    end
  end
end

The above code will work as a patch if you want to try it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions