Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Roact should warn when setting state on a component from outside the tree #283

Open
oltrep opened this issue Jul 13, 2020 · 1 comment
Open

Comments

@oltrep
Copy link
Contributor

oltrep commented Jul 13, 2020

React does it here.

In Roact, doing this does not provide any useful warnings/errors compared to React.

function Component:init()
    self.props.navigation.setParams({
        headerText = "something"
    })
end
@oltrep
Copy link
Contributor Author

oltrep commented Jul 14, 2020

From the link I put in the previous message, we can translate the test to something similar in Lua:

local Foo = Roact.Component:extend("Foo")
local setValue

function Foo:init()
    setValue = function(newValue)
        self:setState({
            value = newValue,
        })
    end
    self.state = {
        value = "1",
    }
end

function Foo:render()
    return Roact.createElement("TextLabel", {
       Text = self.state.value,
    })
end

local function Bar(props)
    if props.triggerUpdate then
        setValue("3")
    end
    return Roact.createElement("TextLabel", {
        Text = "2",
    })
end

Then if you mount this twice:

local folder = Instance.new("Folder")

local fragment = Roact.createFragment({
    Foo = Roact.createElement(Foo),
    Bar = Roact.createElement(Bar, {triggerUpdate = false}),
})

local handle = Roact.mount(fragment, folder, "Test")

local updatedFragment = Roact.createFragment({
    Foo = Roact.createElement(Foo),
    Bar = Roact.createElement(Bar, {triggerUpdate = true}),
})

Roact.update(handle, updatedFragment)

If you do this in React, you would get this error Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state..

Right now in Roact no warning or error is shown.

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

No branches or pull requests

1 participant