Vue · Middle · Question 14 of 75
How do you keep provided state reactive but prevent descendants from mutating it?
const user = ref({ name: "Ann" });
provide("user", readonly(user));
provide("updateUser", (patch) => Object.assign(user.value, patch));- ✓Correct answer: Provide a
readonlyversion and a separate function for changes - 2Provide a plain object copy
- 3Provided values are read-only by default
Explanation
Keeping mutations in the provider keeps the data flow traceable. Descendants that need to change state call the provided function.