Class: React::Component::State

Inherits:
Object
  • Object
show all
Includes:
Native::Wrapper
Defined in:
lib/react/component/state.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/react/component/state.rb', line 6

def method_missing(key, *args, &block)
  if `args.length > 0`
    new_state = `{}`
    new_state.JS[(`key.endsWith('=')` ? key.chop : key)] = args[0]
    if block_given?
      @native.JS.setState(new_state, `function() { block.$call(); }`)
    else
      @native.JS.setState(new_state, `null`)
    end
  else
    return nil if `typeof #@native.state[key] === "undefined"`
    @native.JS[:state].JS[key]
  end
end

Instance Method Details

#set_state(updater, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/react/component/state.rb', line 21

def set_state(updater, &block)
  new_state = `{}`
  updater.keys.each do |key|
    new_state.JS[key] = updater[key]
  end
  if block_given?
    @native.JS.setState(new_state, `function() { block.$call(); }`)
  else
    @native.JS.setState(new_state, `null`)
  end
end

#sizeObject



33
34
35
# File 'lib/react/component/state.rb', line 33

def size
  `Object.keys(#@native.state).length`;
end

#to_nObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/react/component/state.rb', line 37

def to_n
  %x{
    var new_native = {};
    for (var key in #@native.state) {
      if (typeof #@native.state[key].$to_n !== "undefined") {
        new_native[key] = #@native.state[key].$to_n();
      } else {
        new_native[key] = #@native.state[key];
      }
    }
    return new_native;
  }
end