Skip to content

Addition of a declarative API alongside the currently imperative one #14

@Nidoxs

Description

@Nidoxs

ReactFlow does not currently support on-the-fly changing of spring goals declaratively (similar to RoactSpring, see the documentation relating to its declarative API here).

I think this would be a prudent addition to ReactFlow, as it helps cut down some of the imperative calls to update / start the animation when you just want the spring's goal to reflect React state. I think there's space for both a declarative and imperative API here. I also think this change would make sure ReactFlow respects the declarative nature of React, as currently this library is focused on the imperative from an end user perspective.

Current Version

This example works but is a little unnecessarily cluttered in my opinion:

local ReactFlow = require(game.ReplicatedStorage.Packages.ReactFlow)
local React = require(game.ReplicatedStorage.Packages.React)

local function MyComponent(props)
    local hovered = React.useState(false)

    local color, setColor = ReactFlow.useSpring({
        start = Color3.fromRGB(0,0,0),
        target = Color3.fromRGB(255,255,255)
    })

    -- if the color was based on the hovered state, you'd have to update it like so
    if hovered then
        setColor({
            target = Color3.fromRGB(255,255,255)
        })
    else
        setColor({
            target = Color3.fromRGB(150,150,150)
        })
    end

    -- or even this:
    setColor({
        target = if hovered then Color3.fromRGB(255,255,255) else Color3.fromRGB(150,150,150)
    })

    return e("TextButton", {
        BackgroundColor3 = color
    })
end

Proposed Version

Shorter and easier to read:

local ReactFlow = require(game.ReplicatedStorage.Packages.ReactFlow)
local React = require(game.ReplicatedStorage.Packages.React)

local function MyComponent(props)
    local hovered = React.useState(false)

    local color, setColor = ReactFlow.useSpring({
        start = Color3.fromRGB(0,0,0),
        target = if hovered then Color3.fromRGB(255,255,255) else Color3.fromRGB(150,150,150)
    })

    return e("TextButton", {
        BackgroundColor3 = color
    })
end

The imperative API should be retained as there are certainly use cases for it in my opinion. In my own experience, I've had to handle animations of React UI based on more than just a single React state (like hovered etc) so I think keeping what you've got so far is wise, this would be an addition on top of it to support on-the-fly updating of animation goals via the hook props tables. 😄

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions