Need Help with Roact/Rodux Issue

I started learning Roact / React-Lua around 3 months ago, and today I’m learning Rodux and RoactRodux.
It seems like I followed the tutorial exactly as shown in Learning roactrodux - #2 by SubtotalAnt8185,
but it’s not working, and I’m not sure what’s wrong.
I appreciate any help in advance!

-- Store.luau
local Rodux = require(script.Parent.Rodux)

return Rodux.Store.new(function(state, action)
    state = state or {
        Text = "Hello",
    }

    if action.type == "ChangeText" then
        state.Text = action.Text
    end

    warn(state)

    return state
end)
-- MyComponent.luau
local Roact = require(script.Parent.Roact)

local MyComponent = Roact.Component:extend("MyComponent")

function MyComponent:render()
    local props = self.props

    return Roact.createElement("ScreenGui", {
        ResetOnSpawn = false,
    }, {
        Label = Roact.createElement("TextLabel", {
            Size = UDim2.fromScale(0.5, 0.5),
            Text = props.Text,
        })
    })
end

return MyComponent
-- main.client.luau
local Players = game:GetService("Players")

local Roact = require(script.Roact)
local RoactRodux = require(script.RoactRodux)
local Store = require(script.Store)
local MyComponent = require(script.MyComponent)

MyComponent = RoactRodux.connect(function(state, _props)
    return state
end, function(_dispatch)
    return {}
end)(MyComponent)

local roactroduxTree = Roact.createElement(RoactRodux.StoreProvider, {
    store = Store,
}, {
    coolGui = Roact.createElement(MyComponent),
})

Roact.mount(roactroduxTree, Players.LocalPlayer.PlayerGui)

task.wait(2)

Store:dispatch({
    type = "ChangeText",
    Text = "Changed Text!",
})

It seems like you’re trying to integrate Roact and Rodux (with RoactRodux) to manage state in your Roblox game, but you’re running into an issue. Based on the code you’ve shared, I’ll walk through a few things and highlight potential areas for improvement.

Here’s a step-by-step breakdown to address possible issues:

1. Store (State Management):

Your Store.luau looks mostly fine, but it’s important to note that you should return an updated state object instead of modifying the original state directly. Additionally, when you use warn() for debugging, make sure you’re inspecting the correct parts of the state.

Here’s an updated version of the store:

Copiar código

-- Store.luau
local Rodux = require(script.Parent.Rodux)

return Rodux.Store.new(function(state, action)
    -- Default state initialization
    state = state or {
        Text = "Hello",
    }

    -- Action handler
    if action.type == "ChangeText" then
        state = {
            Text = action.Text, -- Returning a new state object
        }
    end

    -- Debugging with 'warn'
    warn(state)

    return state
end)

2. Component:

Your MyComponent.luau looks like it’s correctly rendering the TextLabel. However, you should ensure that props are passed properly to the component and update accordingly when state changes. The Roact.createElement for TextLabel should be correctly set to show the Text prop.

Here’s the code again for the component:

Copiar código

-- MyComponent.luau
local Roact = require(script.Parent.Roact)

local MyComponent = Roact.Component:extend("MyComponent")

function MyComponent:render()
    local props = self.props

    return Roact.createElement("ScreenGui", {
        ResetOnSpawn = false,
    }, {
        Label = Roact.createElement("TextLabel", {
            Size = UDim2.fromScale(0.5, 0.5),
            Text = props.Text, -- Make sure Text is coming from props
        })
    })
end

return MyComponent

3. Connecting Store to Component (Main Logic):

In the main.client.luau file, you’re using RoactRodux.connect to map state to props. It looks mostly correct, but you’re not dispatching any actions, which is fine if you’re just displaying state, but if you want the ability to update the state, you’ll need to handle dispatching actions as well.

Here’s the revised code:

Copiar código

-- main.client.luau
local Players = game:GetService("Players")

local Roact = require(script.Roact)
local RoactRodux = require(script.RoactRodux)
local Store = require(script.Store)
local MyComponent = require(script.MyComponent)

-- Connect component to the store
MyComponent = RoactRodux.connect(function(state, _props)
    return {Text = state.Text}  -- Mapping the 'Text' property from the state to props
end, function(dispatch)
    return {
        -- Dispatch actions if necessary
        changeText = function(text)
            dispatch({
                type = "ChangeText",
                Text = text,
            })
        end
    }
})(MyComponent)

-- Wrap the component with the StoreProvider and mount
local roactroduxTree = Roact.createElement(RoactRodux.StoreProvider, {
    store = Store,
}, {
    coolGui = Roact.createElement(MyComponent),
})

Roact.mount(roactroduxTree, Players.LocalPlayer.PlayerGui)

task.wait(2)

Key Changes:

  1. Mapping the Text state to props: In the connect function, we map the Text state to the props of MyComponent. This ensures that your component will react to changes in the state (and update the TextLabel accordingly).
  2. Dispatching actions: I’ve added a changeText function to the dispatch object. If you want to update the state, you will need to dispatch the action, but you can remove it if you don’t need it yet.

4. Debugging Tips:

  • Check the output console: Ensure that the state is being properly logged with warn(state) in your store. This will tell you if the store is receiving and updating the state.
  • Check if the component is rendered: If you don’t see the TextLabel, ensure that your GUI is mounted correctly in the PlayerGui.
  • Inspect Props: You can add warn(props) in your MyComponent:render() method to check if the Text is actually coming through from the state.

Conclusion:

By fixing the handling of the state and ensuring the props are mapped properly, you should see your GUI update correctly when the state changes. If you want to dispatch actions later on (e.g., when a button is clicked), you can use the dispatch function provided by RoactRodux.connect and handle state updates that way.

Let me know if this resolves your issue or if you’re encountering another error!

1 Like

The issue was in the 10th line of main.client.luau.
Thank you for helping me resolve this issue!