-
-
Notifications
You must be signed in to change notification settings - Fork 144
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
question: What is this prop vs. state idea #291
Comments
These are the notes that I have found and added to on state vs. property vecty:"prop"Related issue: #209 What does the This allows:
If the value of a variable is set by the parent and is not changed within the methods of the current struct then is should be set as a prop variable (i.e. is will have the These rules apply to all type of variables and include types: struct, func, pointer, interface, etc. Warning Notes to selfTo be able to use a pointer to the correct version of state variables you will need to make the pointer from the first instance of the structure. This applies to func, pointers, interfaces. This is because the old (original) structure is the master for all state variables. This is not recommended. More notes on Regarding using public vs. private fieldsI think that can be confusing because you are then applying semantic meaning to public vs. private as being "public to the component" and "private to the component" rather than to the package. Effectively, you would be replacing What matters to me the most is the mental model of the Vecty API. Coming from a React background, I just always expect the parent to be the ruler of the most updated values to a given prop, regardless of how many times it rerenders. If a child wants to keep its own state, then it just copies the value to another field during Mount time. I agree about the private/public comment you made. |
This gets asked a lot, what @VinceJnz said seems all accurate though, but actually there's a very simple answer.
Example: When to use
|
Thanks for the explanation @slimsag! I just noticed I was doing some hacky workarounds to get things that should have been props to render, like calling a struct's render method instead of embedding it! All in all I think I've understood when to use one or the other. Here's an example for whomever wants to play around with slimsags Alarm Clock idea, it really helped drive the idea home. Click on the time to cause a rerender on the parent's (AlarmClock) side. Click to see code examplepackage main
import (
"time"
"github.com/hexops/vecty"
"github.com/hexops/vecty/elem"
"github.com/hexops/vecty/event"
)
func main() {
vecty.SetTitle("Hello Vecty!")
vecty.RenderBody(&PageView{})
}
// PageView is our main page component.
type PageView struct {
vecty.Core
}
// Render implements the vecty.Component interface.
func (p *PageView) Render() vecty.ComponentOrHTML {
return elem.Body(
vecty.Text("Hello Vecty!"),
elem.Paragraph(&AlarmClock{}),
)
}
type AlarmClock struct {
vecty.Core
}
func (p *AlarmClock) Render() vecty.ComponentOrHTML {
t := &CurrentTime{
Time: time.Now(),
}
return elem.Div(
vecty.Markup(event.Click(func(e *vecty.Event) {
vecty.Rerender(p)
})),
t, // Once can also just call t.Render() and use the result to not have to use `props` (a.k.a BAD PRACTICE)
)
}
type CurrentTime struct {
vecty.Core
Time time.Time `vecty:"prop"`
}
func (p *CurrentTime) Render() vecty.ComponentOrHTML {
return elem.Span(
vecty.Text(p.Time.String()),
)
} |
I've noticed component structs sometimes have fields with
vecty:"prop"
tag. I've also come across the issue #209 and the generics issue #277.What is this separation?Why is it necessary? I've built vecty apps without it just fine.
The text was updated successfully, but these errors were encountered: