How do I generate more than one dom element from a react.rb component

84 views Asked by At

I am trying to get the following code working:

class HelloWorld < React::Component::Base
  # stuff
  def render
    div { "hello" }
    div { "goodby" }
  end
end

but I keep getting this error:

Exception raised while rendering #<HelloWorld:0x489> RuntimeError: a components render method must generate and return exactly 1 element or a string

1

There are 1 answers

0
Mitch VanDuyn On

React.rb components (like react.js) can only render a single root dom element.

To generate multiple elements you need to wrap the elements in a div, for example:

class HelloWorld < React::Component::Base
  # stuff
  def render
    div do
      div { "hello" }
      div { "goodby" }
    end
  end
end