Skip to content

Allow default content in content_tag in case of noscript/render failure #59

Description

@jtokoph

I would like to specify some default content that renders serverside within the content tag generated by the react_component helper. I've noticed that it defaults to calling content_tag with a nil body and created my own react_component_with_defaults helper like so:

def react_component_with_defaults(name, props = {}, options = {}, &block)
    tag = options.delete(:tag) || :div
    data = { data: { 'react-class' => name, 'react-props' => props.to_json } }

    content = block_given? ? capture(&block) : nil
    content_tag(tag, content, options.deep_merge(data))
end

This would theoretically allow me to pass a block with default content to render serverside as a fallback in the event of someone having js disabled or a mounting issue when the react js finally executes.

<%= react_component_with_defaults 'SomeComponent', { some_prop: 1 } do %>
    <div>default content here</div>
<% end %>

Unfortunately the javascript side of webpacker-react prevents rendering if there is any innerHTML to the content tag (See relevant line)

Are there any arguments against adding this as a feature? Or is this just something that react wouldn't like with automatic hydration attempts? I'm not knowledgeable enough on react internals to know if this is a bad idea or not.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions