R
roblox-ts10h ago
kv_

Atom with array does not update react components

any obvious reason my react component is not re-rendering when I update the atom? is this because the atom is holding an array?
// flamework controller...
private _alerts: Charm.Atom<AlertContext[]>

updateMethod(alert) {
const t = this._alerts();
t.push(alert);
this._alerts(t);
}
// flamework controller...
private _alerts: Charm.Atom<AlertContext[]>

updateMethod(alert) {
const t = this._alerts();
t.push(alert);
this._alerts(t);
}
// react component...
const c = ( props: { alerts: Charm.Atom<AlertContext[]> }) => {
const alerts = useAtom(props.alerts);

useEffect(() => {
print("the state changed") // does not print on calls to updateMethod
}, alerts)
}
// react component...
const c = ( props: { alerts: Charm.Atom<AlertContext[]> }) => {
const alerts = useAtom(props.alerts);

useEffect(() => {
print("the state changed") // does not print on calls to updateMethod
}, alerts)
}
Solution:
```ts private _alerts: Charm.Atom<AlertContext[]> updateMethod(alert) { const t = this._alerts((old) => {...
Jump to solution
10 Replies
PepeElToro41
PepeElToro4110h ago
charm is immutable
kv_
kv_OP10h ago
?
PepeElToro41
PepeElToro4110h ago
No description
PepeElToro41
PepeElToro4110h ago
ai slop letsgooo that means you cant change tables you need to clone them and then change them
kv_
kv_OP10h ago
oh yeah i see t is probably a reference to the original table ok
PepeElToro41
PepeElToro4110h ago
yeah use @rbxts/sift has the same methods but with immutable data
kv_
kv_OP10h ago
can i not just t = table.clone()
Solution
PepeElToro41
PepeElToro4110h ago
private _alerts: Charm.Atom<AlertContext[]>

updateMethod(alert) {
const t = this._alerts((old) => {
return sift.Array.push(old, alert)
});
}
private _alerts: Charm.Atom<AlertContext[]>

updateMethod(alert) {
const t = this._alerts((old) => {
return sift.Array.push(old, alert)
});
}
kv_
kv_OP10h ago
oo ok thanks :)
PepeElToro41
PepeElToro4110h ago
np :))

Did you find this page helpful?