Module: React::Component

Defined in:
lib/react/component.rb

Defined Under Namespace

Modules: API, ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/react/component.rb', line 80

def method_missing(name, *args, &block)
  unless (React::HTML_TAGS.include?(name) || name == 'present' || name == '_p_tag')
    return super
  end

  if name == "present"
    name = args.shift
  end

  if name == "_p_tag"
    name = "p"
  end

  @buffer = [] unless @buffer
  if block
    current = @buffer
    @buffer = []
    result = block.call
    element = React.create_element(name, *args) { @buffer.count == 0 ? result : @buffer }
    @buffer = current
  else
    element = React.create_element(name, *args)
  end

  @buffer << element
  element
end

Class Method Details

.included(base) ⇒ Object



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

def self.included(base)
  base.include(API)
  base.include(React::Callbacks)
  base.class_eval do
    class_attribute :init_state, :validator
    define_callback :before_mount
    define_callback :after_mount
    define_callback :before_receive_props
    define_callback :before_update
    define_callback :after_update
    define_callback :before_unmount
  end
  base.extend(ClassMethods)
end

Instance Method Details

#component_did_mountObject



48
49
50
# File 'lib/react/component.rb', line 48

def component_did_mount
  self.run_callback(:after_mount)
end

#component_did_update(prev_props, prev_state) ⇒ Object



64
65
66
# File 'lib/react/component.rb', line 64

def component_did_update(prev_props, prev_state)
  self.run_callback(:after_update, Hash.new(prev_props), Hash.new(prev_state))
end

#component_will_mountObject



44
45
46
# File 'lib/react/component.rb', line 44

def component_will_mount
  self.run_callback(:before_mount)
end

#component_will_receive_props(next_props) ⇒ Object



52
53
54
# File 'lib/react/component.rb', line 52

def component_will_receive_props(next_props)
  self.run_callback(:before_receive_props, Hash.new(next_props))
end

#component_will_unmountObject



68
69
70
# File 'lib/react/component.rb', line 68

def component_will_unmount
  self.run_callback(:before_unmount)
end

#component_will_update(next_props, next_state) ⇒ Object



60
61
62
# File 'lib/react/component.rb', line 60

def component_will_update(next_props, next_state)
  self.run_callback(:before_update, Hash.new(next_props), Hash.new(next_state))
end

#emit(event_name, *args) ⇒ Object



40
41
42
# File 'lib/react/component.rb', line 40

def emit(event_name, *args)
  self.params["_on#{event_name.to_s.event_camelize}"].call(*args)
end

#initialize(native_element) ⇒ Object



23
24
25
# File 'lib/react/component.rb', line 23

def initialize(native_element)
  @native = native_element
end

#p(*args, &block) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/react/component.rb', line 72

def p(*args, &block)
  if block || args.count == 0 || (args.count == 1 && args.first.is_a?(Hash))
    _p_tag(*args, &block)
  else
    Kernel.p(*args)
  end
end

#paramsObject



27
28
29
# File 'lib/react/component.rb', line 27

def params
  Hash.new(`#{@native}.props`)
end

#refsObject



31
32
33
# File 'lib/react/component.rb', line 31

def refs
  Hash.new(`#{@native}.refs`)
end

#should_component_update?(next_props, next_state) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/react/component.rb', line 56

def should_component_update?(next_props, next_state)
  self.respond_to?(:needs_update?) ? self.needs_update?(Hash.new(next_props), Hash.new(next_state)) : true
end

#stateObject



35
36
37
38
# File 'lib/react/component.rb', line 35

def state
  raise "No native ReactComponent associated" unless @native
  Hash.new(`#{@native}.state`)
end