@@ -29,7 +29,14 @@ def self.create_element(type, properties = {}, &block)
29
29
30
30
def self . render ( element , container )
31
31
container = `container.$$class ? container[0] : container`
32
- component = Native ( `React.render(#{ element . to_n } , container, function(){#{ yield if block_given? } })` )
32
+ if !( `typeof ReactDOM === 'undefined'` )
33
+ component = Native ( `ReactDOM.render(#{ element . to_n } , container, function(){#{ yield if block_given? } })` ) # v0.15+
34
+ elsif !( `typeof React.renderToString === 'undefined'` )
35
+ component = Native ( `React.render(#{ element . to_n } , container, function(){#{ yield if block_given? } })` )
36
+ else
37
+ raise "render is not defined. In React >= v15 you must import it with ReactDOM"
38
+ end
39
+
33
40
component . class . include ( React ::Component ::API )
34
41
component
35
42
end
@@ -39,22 +46,51 @@ def self.is_valid_element(element)
39
46
end
40
47
41
48
def self . render_to_string ( element )
42
- React ::RenderingContext . build { `React.renderToString(#{ element . to_n } )` }
49
+ if !( `typeof ReactDOMServer === 'undefined'` )
50
+ React ::RenderingContext . build { `ReactDOMServer.renderToString(#{ element . to_n } )` } # v0.15+
51
+ elsif !( `typeof React.renderToString === 'undefined'` )
52
+ React ::RenderingContext . build { `React.renderToString(#{ element . to_n } )` }
53
+ else
54
+ raise "renderToString is not defined. In React >= v15 you must import it with ReactDOMServer"
55
+ end
43
56
end
44
57
45
58
def self . render_to_static_markup ( element )
46
- React ::RenderingContext . build { `React.renderToStaticMarkup(#{ element . to_n } )` }
59
+ if !( `typeof ReactDOMServer === 'undefined'` )
60
+ React ::RenderingContext . build { `ReactDOMServer.renderToStaticMarkup(#{ element . to_n } )` } # v0.15+
61
+ elsif !( `typeof React.renderToString === 'undefined'` )
62
+ React ::RenderingContext . build { `React.renderToStaticMarkup(#{ element . to_n } )` }
63
+ else
64
+ raise "renderToStaticMarkup is not defined. In React >= v15 you must import it with ReactDOMServer"
65
+ end
47
66
end
48
67
49
68
def self . unmount_component_at_node ( node )
50
- `React.unmountComponentAtNode(node.$$class ? node[0] : node)`
69
+ if !( `typeof ReactDOM === 'undefined'` )
70
+ `ReactDOM.unmountComponentAtNode(node.$$class ? node[0] : node)` # v0.15+
71
+ elsif !( `typeof React.renderToString === 'undefined'` )
72
+ `React.unmountComponentAtNode(node.$$class ? node[0] : node)`
73
+ else
74
+ raise "unmountComponentAtNode is not defined. In React >= v15 you must import it with ReactDOM"
75
+ end
51
76
end
52
77
53
78
end
54
79
55
80
Element . instance_eval do
81
+
56
82
class ::Element ::DummyContext < React ::Component ::Base
57
83
end
84
+
85
+ def self . find ( selector )
86
+ selector = selector . dom_node if selector . respond_to? :dom_node rescue selector
87
+ `$(#{ selector } )`
88
+ end
89
+
90
+ def self . []( selector )
91
+ find ( selector )
92
+ end
93
+
58
94
def render ( &block )
59
95
React . render ( React ::RenderingContext . render ( nil ) { ::Element ::DummyContext . new . instance_eval &block } , self )
60
96
end
0 commit comments