Component not rerendering when observer is updated #3488
-
Hi, I have a component which is not being rerendered when the state updates and I can't figure out why, so I thought I'd ask you lovely people for help. const KeyComponent: React.FC<KeyProps> = observer(({ scaleKey, scaleType }) => {
const ScalesStore = useContext(ScalesContext);
const isSelected = ScalesStore.scales[scaleType].includes(scaleKey);
const handlePress = () => {
ScalesStore.toggleKey(scaleType, scaleKey);
};
return (
<KeyPressable selected={isSelected} onPress={handlePress}>
<PressableText selected={isSelected}>{scaleKey}</PressableText>
</KeyPressable>
);
}); This is the state class class Scales {
scales;
constructor() {
makeAutoObservable(this);
this.scales = {
major: [Key.C],
minor: [Key.A],
};
}
isSelected(scaleType: ScaleType, key: Key) {
return this.scales[scaleType].includes(key);
}
toggleKey(scaleType: ScaleType, key: Key) {
if (this.isSelected(scaleType, key)) {
this.scales[scaleType] = this.scales[scaleType].filter((k) => k !== key);
} else {
this.scales[scaleType].push(key);
}
}
} When I press the button, Thank you for your help 🙏 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey, did you check that your transpiler is configured correctly? https://mobx.js.org/installation.html#use-spec-compliant-transpilation-for-class-properties There is a JS oneliner, please run it to check. If there is no issue, it's better to create a reproducible codesandbox with the issue here: https://codesandbox.io/s/minimal-mobx-react-project-ppgml?file=/index.js |
Beta Was this translation helpful? Give feedback.
Hey, did you check that your transpiler is configured correctly? https://mobx.js.org/installation.html#use-spec-compliant-transpilation-for-class-properties
There is a JS oneliner, please run it to check. If there is no issue, it's better to create a reproducible codesandbox with the issue here: https://codesandbox.io/s/minimal-mobx-react-project-ppgml?file=/index.js