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
Button input onclick
not updating as expected in view
#3
Comments
Thanks for reporting, In your code, the closure on the Here is the modified view of the code, which works as they way you wanted. fn view(&self) -> Node<Msg> {
div(
[],
[
div(
[],
[input(
[
r#type("button"),
value(format!("VIEW_COUNT: {}",self.number)),
onclick(move |_| {
VIEW_COUNT.fetch_add(1, Ordering::SeqCst);
let vc = VIEW_COUNT.load(Ordering::SeqCst);
sauron::log(format!("Button is clicked (VIEW_COUNT = {})", vc));
Msg::Click(vc)
}),
],
[],
)],
),
div(
[],
[text(format!(
"VIEW_COUNT: {}",
VIEW_COUNT.load(Ordering::SeqCst)
))],
),
div(
[],
[text(format!(
"number: {}",
self.number
))],
),
],
)
} |
Thanks for the response. Although the proposed solution works in this particular case, I don't think that it generalises to the actual problem I'm trying to solve. In my particular case, I need the The originally observed behaviour, therefore, still surprises me. Inside Relevant snippet as follows:
|
That's the behavior of the framework for optimization reason. The event listener is attached only one time at the time it is created and is never replaced even with a different closure/function, as there is no easy way to compare closure if they have changed or not. The attributes(ie: Maybe you could also passed the |
Thanks. The behaviour now makes sense. What now does not make sense is why your example
|
It works beacuase idx is a |
The
|
I'm closing this issue now, since this not going anywhere, and is not related to |
Not trying to be annoying, but a port of the same code to Yew seems to work just fine: https://github.com/gesterhuizen/yew-onclick I'm really just trying to understand what I'm doing wrong here and what I'm missing. Unfortunately, the explanations given above does not make sense to me (and the issue does not seem fundamentally tied to the way I'm using an atomic). |
I realized that the root cause of this issue is caused from using |
Disclaimer: I am very new to Rust, so it is quite likely that the behaviour that I'm describing is totally expected and I'm just getting stumped by my lack of clear understanding of the language.
I'm seeing unexpected behaviour in how a button input
onclick
handler functions. The closure is supposed to change during each rerender (i.e.view
invocation), based on application state. However, the observed behaviour seems to indicate that theonclick
handler that is first set on the input is always used. This is despite theonclick
being update multiple times inview
. In the same update, the inputvalue
is also updated, and this is correctly reflected.I made a small app, based off of the
minimal
Sauron example, which illustrates this: https://github.com/gesterhuizen/sauron-onclickThe README from that project is duplicated below:
Steps to reproduce
Expected
VIEW_COUNT
on the button and in the text field should be3
. This is expected because the button value and text field reflects the value thatVIEW_COUNT
held at the time that the view was rendered.number
field should be3
. This is expected because, when the button is clicked, thenumber
field is updated with the value thatVIEW_COUNT
held at the time that the view was rendered. We assign a newonclick
closure each time the view is rendered (https://github.com/gesterhuizen/sauron-onclick/blob/master/src/lib.rs#L40):Actual
VIEW_COUNT
on the button and in the text field is3
(as expected).number
field displays1
(and stays in this state, no matter how many times the button is clicked). This is unexpected. It seems like firstonclick
handler that was set (i.e. whenVIEW_COUNT
held the value1
) is used even after multiple view rerenders.The text was updated successfully, but these errors were encountered: