How to retrieve function from Roact component

Hello! For my Roact GUI, I would like my component to somehow ‘return’ a function to the parent component, so that the parent component can access it and call it. How can I achieve this? The function will manipulate the bindings of the child component.
Am I meant to store the function in the child component like this: childComponent.funcName, or what?

1 Like

I see that @GrammaticaIly has liked my post, making me believe that I could just do it with childComponent.funcName. I tried coding up a test, but it gave me the error

LocalScript:38: attempt to call a nil value

This was surprising, as I had implemented both a function and a method:

-- ...importing libs...

--//components > square
local square = Roact.Component:extend("Square")

-- Implementing both a method and a function...
function square:init()
	-- here is the function
	self.sayHello = function()
		print("Hello!!")
	end
end

-- here is the method
function square:sayHello()
	print("Hi!")
end

function square:render()
	return Roact.createElement("Frame")
end

--//components > gui
local gui = Roact.Component:extend("GUI")

function gui:render()
	local subComponent = Roact.createElement(square)
	
	local func = function()
		wait(5)
		print(subComponent)
		subComponent.sayHello() -- attempt to call a nil value
	end
	
	spawn(func)
	
	return Roact.createElement("ScreenGui", {}, {
		sub = Roact.createElement(square)
	})
end
--//mounting
local guiHandle = Roact.mount(Roact.createElement(gui), plr.PlayerGui)

Did I do something wrong, or is this not the right way to do this?