reactR icon indicating copy to clipboard operation
reactR copied to clipboard

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

Open stla opened this issue 4 years ago • 0 comments

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.

stla avatar Jun 12 '21 12:06 stla