Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stla opened this issue Jun 12, 2021 · 0 comments
Open

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

stla opened this issue Jun 12, 2021 · 0 comments

Comments

@stla
Copy link
Contributor

stla commented Jun 12, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant