博文

目前显示的是 十月 22, 2025的博文

🔥 (240) Forcing a Component to Update, Creating an If...Else Component, and where do you put shared state?

图片
Read this on my blog Hey there! Another week, another newsletter. Enjoy! — Michael 🔥 Forcing a Component to Update What do you do if a component isn't updating the way it should? Likely, this is caused by a misunderstanding and misuse of the reactivity system. But let's look at a quick solution using forceUpdate : import { getCurrentInstance } from 'vue' ; const methodThatForcesUpdate = () => { // ... const instance = getCurrentInstance (); instance . proxy . forceUpdate (); // ... }; Using the Options API instead: export default { methods : { methodThatForcesUpdate () { // ... this . $forceUpdate (); // Notice we have to use a $ here // ... } } } Now, here comes the sledgehammer if the previous approach doesn't work. I do not recommend using this approach . However, sometimes you just need to get your code t...