roact icon indicating copy to clipboard operation
roact copied to clipboard

Add naming for functional components

Open LPGhatguy opened this issue 7 years ago • 2 comments

I want to recommend using functional components by default, but debugging with them is a huge pain because they don't have names.

I'd like to introduce a little utility that keeps a map of functional components to their names:

local function MyComponent(props)
    -- ...
end

Roact.setComponentName(MyComponent, "MyComponent")

or maybe even something like

local MyComponent = Roact.FunctionalComponent("MyComponent", function(props)
    -- ...
end)

but then we sort of lose the idea that functional components are just simple functions.

LPGhatguy avatar Mar 09 '18 23:03 LPGhatguy

It's easy to implement setComponentName, but that raises the question: where is this name read? Is it intended to be used in debugging with print statements or the Lua debugger? We can't make tostring return the component's name, and wrapping the functional component in some way runs into the same issue as #9.

AmaranthineCodices avatar Mar 13 '18 23:03 AmaranthineCodices

I've mulled over this problem for a couple months now.

I think we should introduce a new library that allows you to give names to things. It would basically just be a global weak map from any to string.

Roact can then optionally load this library and take advantage of it if you have it installed and have named your functional components with it.

I think this is too generically common of a problem to integrate directly into Roact, since naming things in general is useful!

LPGhatguy avatar May 05 '18 00:05 LPGhatguy