Reactive state in textfield not working #763
-
|
hi, I try to playaround for reactive state in TextField. when I try to put any text, it didn't reflect. I assume could be reactive issue? I try to run this by using perry run and perry dev. below is my code: and here is the info for my perry: Environment Checks and here is my repo: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hey @wansidqi — thanks for the clear repro. You've hit a real gap in Perry's UI reactivity, not a bug in your code. What's happeningThe problematic line is: Text(`current state for text: ${text.value}`)
Internally, Perry's runtime already has a Workaround for nowUse import {
App, Button, HStack, State, stateBindTextfield, stateOnChange,
Text, TextField, textSetString, VStack, widgetSetBackgroundColor,
setCornerRadius, textSetFontWeight,
} from 'perry/ui';
function main(): void {
const widgetStyleButton = Button("widget style button", () => console.log('click'));
widgetSetBackgroundColor(widgetStyleButton, 9 / 255, 241 / 255, 180 / 255, 0.8);
setCornerRadius(widgetStyleButton, 8);
const bold = Text("Bold");
textSetFontWeight(bold, 20, 1.0);
const text = State("");
const field = TextField("Type something", (value) => text.set(value));
stateBindTextfield(text, field);
const inlineStyleButton = Button("set hello world", () => text.set("Hello, World!"));
widgetSetBackgroundColor(inlineStyleButton, 245 / 255, 40 / 255, 145 / 255, 0.8);
// Reactive label: re-render whenever `text` changes.
const stateLabel = Text("current state for text: ");
stateOnChange(text, (v) => {
textSetString(stateLabel, `current state for text: ${v}`);
});
App({
title: "Layout Demo",
width: 400,
height: 700,
body: VStack(16, [
Text("Header"),
HStack(8, [widgetStyleButton]),
HStack(12, [field]),
HStack(8, [bold, inlineStyleButton]),
stateLabel,
Text("Footer"),
]),
});
}
main();That gives you a label that updates live as you type and also when the button fires Tracking the real fix in #764. |
Beta Was this translation helpful? Give feedback.
Hey @wansidqi — thanks for the clear repro. You've hit a real gap in Perry's UI reactivity, not a bug in your code.
What's happening
The problematic line is:
text.valueis read once at construction time (returns"") and the resulting string is baked into theTextwidget. Subsequenttext.set(...)calls update the state cell, and yourstateBindTextfield(text, field)correctly two-way-binds the input — but the standaloneTextdoes not re-render, because nothing subscribed it to the state cell.Internally, Perry's runtime already has a
perry_ui_state_bind_text_templateprimitive for exactly this use case, but the compile-time pass that auto-rewrites