Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Binding in Components #1074

Merged
merged 3 commits into from
Mar 25, 2024
Merged

Add support for Binding in Components #1074

merged 3 commits into from
Mar 25, 2024

Conversation

TimLariviere
Copy link
Member

Components only supports local state through the use of Context.State(defaultValue).
With Context.Binding(stateValue), a component can take a dependency on the local state of another component and be able to listen to changes as well as update the value, triggering a refresh of both components.

Usage:

// Children
let firstNameView (value: StateValue<string>) =
    Component() {
        let! firstName = Context.Binding(value)
        Entry(firstName.Current, firstName.Set)
    }

let lastNameView (value: StateValue<string>) =
    Component() {
        let! lastName = Context.Binding(value)
        Entry(lastName.Current, lastName.Set)
    }

// Parent
let parentView () =
    Component() {
        // Parent is the one owning the data
        let! firstName = Context.State("")
        let! lastName = Context.State("")

        VStack() {
            Label($"Full name is {firstName.Current} {lastName.Current}")
            firstNameView firstName
            lastNameView lastName
    }

Copy link
Member

@edgarfgp edgarfgp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work

@TimLariviere TimLariviere merged commit 7cb2461 into main Mar 25, 2024
1 check passed
@TimLariviere TimLariviere deleted the component-binding branch March 25, 2024 09:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants