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));

Explanation

Keeping mutations in the provider keeps the data flow traceable. Descendants that need to change state call the provided function.

Further reading

Problem with a question

Vue · Middle · question 14

Test yourself

Take the Vue · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz