Skip to content

Empty attribs in React$Component should be an empty **named** list  #63

Open
@stla

Description

@stla

Hello,

If we build a React component without attributes, we get an empty unnamed list in attribs:

> str(React$Component("Children"))
List of 3
 $ name    : chr "Component"
 $ attribs : list()
 $ children:List of 1
  ..$ : chr "Children"
 - attr(*, "class")= chr [1:2] "reactR_component" "shiny.tag"

This is bad, because this empty list is converted to an empty array in the JavaScript side:

> jsonlite::toJSON(list())
[]

However the attributes must be an object, not an array. So we need an empty named list.

To get such a list, one can use rlang::dots_list. That is, instead of

> reactR:::`$.react_component_builder`
function (x, name) 
{
    function(...) {
        component(name, list(...))
    }
}

one can do:

function (x, name) 
{
    function(...) {
        component(name, rlang::dots_list(...))
    }
}

Then this is fine;

> f <- function(...) component("Name", rlang::dots_list(...))
> str(f("Children"))
List of 3
 $ name    : chr "Name"
 $ attribs : Named list()
 $ children:List of 1
  ..$ : chr "Children"
 - attr(*, "class")= chr [1:2] "reactR_component" "shiny.tag"
 > jsonlite::toJSON(f("Children")$attribs)
{} # yeah, an empty object

Note that using rlang does not add a dependency to the package, because it is already a dependency of htmltools.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions