[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"$KiqIp5Qu8t":3,"vue-tips-\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling":52,"blogToc-\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling":483,"vue-tips-surrounding-\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling":778},{"id":4,"extension":5,"items":6,"meta":49,"stem":50,"__hash__":51},"menu\u002Fmenu.json","json",[7,12,16,20,24,28,32,36,41,45],{"i18nId":8,"to":9,"ariaLabel":10,"mainNavigation":11},"components.menu.home","\u002F","Home",true,{"i18nId":13,"to":14,"ariaLabel":15,"mainNavigation":11},"components.menu.about","\u002Fhire-me","About",{"i18nId":17,"to":18,"ariaLabel":19,"mainNavigation":11},"components.menu.testimonials","\u002Ftestimonials","Testimonials",{"i18nId":21,"to":22,"ariaLabel":23},"components.menu.contact","\u002Fcontact","Contact",{"i18nId":25,"to":26,"ariaLabel":27,"mainNavigation":11},"components.menu.blog","\u002Fblog","Blog",{"i18nId":29,"to":30,"ariaLabel":31,"mainNavigation":11},"components.menu.tips","\u002Fvue-tips","Vue Tips",{"i18nId":33,"to":34,"ariaLabel":35,"mainNavigation":11},"components.menu.projects","\u002Fprojects","Projects",{"i18nId":37,"to":38,"ariaLabel":39,"mainNavigation":40},"components.menu.stats","\u002Fstats","stats",false,{"i18nId":42,"to":43,"ariaLabel":44,"mainNavigation":40},"components.menu.businessCard","\u002Fbusiness-card","Business Card",{"i18nId":46,"to":47,"ariaLabel":48,"mainNavigation":40},"components.menu.publications","\u002Fpublications","Publications",{},"menu","njLT1QoRt2Dv8QWoVmH8xdTAiDywSj0bxBQdmoyWUE0",{"id":53,"title":54,"body":55,"categories":463,"cover":464,"date":465,"description":466,"extension":467,"lastUpdated":468,"meta":469,"navigation":11,"ogImage":468,"path":470,"readingTime":471,"robots":468,"schemaOrg":468,"seo":476,"sitemap":477,"stem":481,"tags":468,"__hash__":482},"vueTips\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling\u002Findex.md","Vue Tip: Use Provide & Inject to Avoid Prop Drilling",{"type":56,"value":57,"toc":459},"minimark",[58,62,66,69,76,93,104,107,118,124,127,143,147,154,324,341,347,350,356,447,455],[59,60],"you-tube-embed",{"url":61},"https:\u002F\u002Fwww.youtube.com\u002Fembed\u002FmIrgiP3Nn_U",[63,64,65],"p",{},"Props are the standard way to pass data from the parent to a child component.",[63,67,68],{},"This works well in simple scenarios but has some drawbacks if a deeply nested component needs data from a distant ancestor component:",[63,70,71],{},[72,73],"img",{"alt":74,"src":75},"Prop Drilling","vue-tips\u002Fprop-drilling.png",[63,77,78,79,83,84,87,88,92],{},"Take a look at the ",[80,81,82],"code",{},"\u003CFooter>"," component: It needs to declare and pass the props to the ",[80,85,86],{},"\u003CDeepChild>"," component, even if it does not care about them. Imagine a longer component chain between the parent and the deep child component; all would be affected along the way. This is called ",[89,90,91],"strong",{},"\"prop drilling\""," and is no fun for us developers.",[63,94,95,96,103],{},"Usually, we solve this problem by using state management solutions like ",[97,98,102],"a",{"href":99,"rel":100},"https:\u002F\u002Fpinia.vuejs.org\u002F",[101],"nofollow","Pinia",".",[63,105,106],{},"But sometimes, we want to share data from a parent component to all its children components without using a store. If you are using Vue 2, you must either use a state management solution or pass the props down to the deeply nested component.",[63,108,109,110,113,114,117],{},"Vue 3 introduced a new concept to solve prop drilling with ",[80,111,112],{},"provide"," and ",[80,115,116],{},"inject"," :",[63,119,120],{},[72,121],{"alt":122,"src":123},"Provide Inject","vue-tips\u002Fprovide-inject.png",[63,125,126],{},"The principle is simple:",[128,129,130,137],"ul",{},[131,132,133,134,136],"li",{},"In the parent component, we declare the variables (props, data, computed, ...), which will be accessible to all descendants via the ",[89,135,112],{}," key.",[131,138,139,140,142],{},"From any child component, we specify the list of injectable variables via the ",[89,141,116],{}," key",[144,145,146],"h2",{"id":112},"Provide",[63,148,149,150,153],{},"To provide data to a component's descendants, we use the ",[80,151,152],{},"provide()"," function:",[155,156,165],"pre",{"className":157,"code":158,"filename":159,"highlights":160,"language":163,"meta":164,"style":164},"language-vue shiki shiki-themes github-light poimandres poimandres","\u003Cscript setup>\nimport { provide } from 'vue'\n\nprovide(\u002F* key *\u002F 'message', \u002F* value *\u002F 'hello!')\n\nconst count = ref(1)\nprovide('counter', count)\n\u003C\u002Fscript>\n","Root.vue",[161,162],4,7,"vue","",[80,166,167,187,219,225,263,268,293,314],{"__ignoreMap":164},[168,169,172,176,180,184],"span",{"class":170,"line":171},"line",1,[168,173,175],{"class":174},"sMzB8","\u003C",[168,177,179],{"class":178},"s8PnL","script",[168,181,183],{"class":182},"sppiw"," setup",[168,185,186],{"class":174},">\n",[168,188,190,194,198,202,205,209,213,216],{"class":170,"line":189},2,[168,191,193],{"class":192},"sRZEA","import",[168,195,197],{"class":196},"sygvE"," {",[168,199,201],{"class":200},"srga6"," provide",[168,203,204],{"class":196}," }",[168,206,208],{"class":207},"sOygg"," from",[168,210,212],{"class":211},"srEIG"," '",[168,214,163],{"class":215},"skTdI",[168,217,218],{"class":211},"'\n",[168,220,222],{"class":170,"line":221},3,[168,223,224],{"emptyLinePlaceholder":11},"\n",[168,226,229,232,235,239,241,244,247,250,253,255,258,260],{"class":227,"line":161},[170,228],"highlight",[168,230,112],{"class":231},"sHQ3f",[168,233,234],{"class":196},"(",[168,236,238],{"class":237},"s1MN7","\u002F* key *\u002F",[168,240,212],{"class":211},[168,242,243],{"class":215},"message",[168,245,246],{"class":211},"'",[168,248,249],{"class":196},", ",[168,251,252],{"class":237},"\u002F* value *\u002F",[168,254,212],{"class":211},[168,256,257],{"class":215},"hello!",[168,259,246],{"class":211},[168,261,262],{"class":196},")\n",[168,264,266],{"class":170,"line":265},5,[168,267,224],{"emptyLinePlaceholder":11},[168,269,271,275,279,282,285,287,291],{"class":170,"line":270},6,[168,272,274],{"class":273},"slWGy","const",[168,276,278],{"class":277},"sGRIV"," count",[168,280,281],{"class":273}," =",[168,283,284],{"class":231}," ref",[168,286,234],{"class":196},[168,288,290],{"class":289},"s6g4C","1",[168,292,262],{"class":196},[168,294,296,298,300,302,305,307,309,312],{"class":295,"line":162},[170,228],[168,297,112],{"class":231},[168,299,234],{"class":196},[168,301,246],{"class":211},[168,303,304],{"class":215},"counter",[168,306,246],{"class":211},[168,308,249],{"class":196},[168,310,311],{"class":174},"count",[168,313,262],{"class":196},[168,315,317,320,322],{"class":170,"line":316},8,[168,318,319],{"class":174},"\u003C\u002F",[168,321,179],{"class":178},[168,323,186],{"class":174},[63,325,326,327,329,330,333,334,337,338,340],{},"The first argument of the ",[80,328,152],{}," function is the ",[89,331,332],{},"injection key",", which can be a string or a ",[80,335,336],{},"Symbol",". The descendant component uses this key to look up the desired value to inject. The second argument is the value, which can be of any type, including reactive state such as refs. Using reactive values in the ",[80,339,152],{}," function establishes a reactive connection to the descendant component that injects the value.",[63,342,343,344,346],{},"As you can see in the above example, it is valid to call ",[80,345,112],{}," multiple times with different injection keys and values.",[144,348,349],{"id":116},"Inject",[63,351,352,353,153],{},"To inject data provided by an ancestor component, we use the ",[80,354,355],{},"inject()",[155,357,361],{"className":157,"code":358,"filename":359,"highlights":360,"language":163,"meta":164,"style":164},"\u003Cscript setup>\nimport { inject } from 'vue'\n\nconst message = inject('message')\nconst count = inject('counter')\n\u003C\u002Fscript>\n","DeepChild.vue",[161,265],[80,362,363,373,392,396,418,439],{"__ignoreMap":164},[168,364,365,367,369,371],{"class":170,"line":171},[168,366,175],{"class":174},[168,368,179],{"class":178},[168,370,183],{"class":182},[168,372,186],{"class":174},[168,374,375,377,379,382,384,386,388,390],{"class":170,"line":189},[168,376,193],{"class":192},[168,378,197],{"class":196},[168,380,381],{"class":200}," inject",[168,383,204],{"class":196},[168,385,208],{"class":207},[168,387,212],{"class":211},[168,389,163],{"class":215},[168,391,218],{"class":211},[168,393,394],{"class":170,"line":221},[168,395,224],{"emptyLinePlaceholder":11},[168,397,399,401,404,406,408,410,412,414,416],{"class":398,"line":161},[170,228],[168,400,274],{"class":273},[168,402,403],{"class":277}," message",[168,405,281],{"class":273},[168,407,381],{"class":231},[168,409,234],{"class":196},[168,411,246],{"class":211},[168,413,243],{"class":215},[168,415,246],{"class":211},[168,417,262],{"class":196},[168,419,421,423,425,427,429,431,433,435,437],{"class":420,"line":265},[170,228],[168,422,274],{"class":273},[168,424,278],{"class":277},[168,426,281],{"class":273},[168,428,381],{"class":231},[168,430,234],{"class":196},[168,432,246],{"class":211},[168,434,304],{"class":215},[168,436,246],{"class":211},[168,438,262],{"class":196},[168,440,441,443,445],{"class":170,"line":270},[168,442,319],{"class":174},[168,444,179],{"class":178},[168,446,186],{"class":174},[63,448,449,450,103],{},"For more details, check the ",[97,451,454],{"href":452,"rel":453},"https:\u002F\u002Fvuejs.org\u002Fguide\u002Fcomponents\u002Fprovide-inject.html",[101],"official docs",[456,457,458],"style",{},"html pre.shiki code .sMzB8, html code.shiki .sMzB8{--shiki-light:#24292E;--shiki-default:#E4F0FB;--shiki-dark:#E4F0FB}html pre.shiki code .s8PnL, html code.shiki .s8PnL{--shiki-light:#22863A;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .sppiw, html code.shiki .sppiw{--shiki-light:#6F42C1;--shiki-light-font-style:inherit;--shiki-default:#91B4D5;--shiki-default-font-style:italic;--shiki-dark:#91B4D5;--shiki-dark-font-style:italic}html pre.shiki code .sRZEA, html code.shiki .sRZEA{--shiki-light:#D73A49;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .sygvE, html code.shiki .sygvE{--shiki-light:#24292E;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .srga6, html code.shiki .srga6{--shiki-light:#24292E;--shiki-default:#ADD7FF;--shiki-dark:#ADD7FF}html pre.shiki code .sOygg, html code.shiki .sOygg{--shiki-light:#D73A49;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .srEIG, html code.shiki .srEIG{--shiki-light:#032F62;--shiki-default:#A6ACCD;--shiki-dark:#A6ACCD}html pre.shiki code .skTdI, html code.shiki .skTdI{--shiki-light:#032F62;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html pre.shiki code .sHQ3f, html code.shiki .sHQ3f{--shiki-light:#6F42C1;--shiki-default:#E4F0FBD0;--shiki-dark:#E4F0FBD0}html pre.shiki code .s1MN7, html code.shiki .s1MN7{--shiki-light:#6A737D;--shiki-light-font-style:inherit;--shiki-default:#767C9DB0;--shiki-default-font-style:italic;--shiki-dark:#767C9DB0;--shiki-dark-font-style:italic}html pre.shiki code .slWGy, html code.shiki .slWGy{--shiki-light:#D73A49;--shiki-default:#91B4D5;--shiki-dark:#91B4D5}html pre.shiki code .sGRIV, html code.shiki .sGRIV{--shiki-light:#005CC5;--shiki-default:#E4F0FB;--shiki-dark:#E4F0FB}html pre.shiki code .s6g4C, html code.shiki .s6g4C{--shiki-light:#005CC5;--shiki-default:#5DE4C7;--shiki-dark:#5DE4C7}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":164,"searchDepth":189,"depth":189,"links":460},[461,462],{"id":112,"depth":189,"text":146},{"id":116,"depth":189,"text":349},[179],"vue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling-static-v1.jpg","2022-11-22","Props are the standard way to pass data from the parent to a child component. Sometimes, we want to share data from a parent component to all its children components without using a store. Provide \u002F Inject solves this problem and avoids prop drilling.","md",null,{},"\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling",{"text":472,"minutes":473,"time":474,"words":475},"2 min read",1.745,104700,349,{"title":54,"description":466},{"loc":470,"images":478,"lastmod":465},[479,480],{"loc":75},{"loc":123},"vue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling\u002Findex","qVwLY4TOjyqTEHZQ4Gtl5_ZbMESdCu0ElxxA5QLEWfU",{"id":484,"title":54,"body":485,"cover":464,"description":466,"extension":467,"meta":769,"navigation":11,"path":470,"robots":468,"seo":772,"sitemap":773,"stem":481,"__hash__":777},"content\u002Fvue-tips\u002Fuse-provide-inject-to-avoid-prop-drilling\u002Findex.md",{"type":56,"value":486,"toc":765},[487,489,491,493,497,505,510,512,518,522,524,534,536,540,651,661,665,667,671,758,763],[59,488],{"url":61},[63,490,65],{},[63,492,68],{},[63,494,495],{},[72,496],{"alt":74,"src":75},[63,498,78,499,83,501,87,503,92],{},[80,500,82],{},[80,502,86],{},[89,504,91],{},[63,506,95,507,103],{},[97,508,102],{"href":99,"rel":509},[101],[63,511,106],{},[63,513,109,514,113,516,117],{},[80,515,112],{},[80,517,116],{},[63,519,520],{},[72,521],{"alt":122,"src":123},[63,523,126],{},[128,525,526,530],{},[131,527,133,528,136],{},[89,529,112],{},[131,531,139,532,142],{},[89,533,116],{},[144,535,146],{"id":112},[63,537,149,538,153],{},[80,539,152],{},[155,541,543],{"className":157,"code":158,"filename":159,"highlights":542,"language":163,"meta":164,"style":164},[161,162],[80,544,545,555,573,577,604,608,624,643],{"__ignoreMap":164},[168,546,547,549,551,553],{"class":170,"line":171},[168,548,175],{"class":174},[168,550,179],{"class":178},[168,552,183],{"class":182},[168,554,186],{"class":174},[168,556,557,559,561,563,565,567,569,571],{"class":170,"line":189},[168,558,193],{"class":192},[168,560,197],{"class":196},[168,562,201],{"class":200},[168,564,204],{"class":196},[168,566,208],{"class":207},[168,568,212],{"class":211},[168,570,163],{"class":215},[168,572,218],{"class":211},[168,574,575],{"class":170,"line":221},[168,576,224],{"emptyLinePlaceholder":11},[168,578,580,582,584,586,588,590,592,594,596,598,600,602],{"class":579,"line":161},[170,228],[168,581,112],{"class":231},[168,583,234],{"class":196},[168,585,238],{"class":237},[168,587,212],{"class":211},[168,589,243],{"class":215},[168,591,246],{"class":211},[168,593,249],{"class":196},[168,595,252],{"class":237},[168,597,212],{"class":211},[168,599,257],{"class":215},[168,601,246],{"class":211},[168,603,262],{"class":196},[168,605,606],{"class":170,"line":265},[168,607,224],{"emptyLinePlaceholder":11},[168,609,610,612,614,616,618,620,622],{"class":170,"line":270},[168,611,274],{"class":273},[168,613,278],{"class":277},[168,615,281],{"class":273},[168,617,284],{"class":231},[168,619,234],{"class":196},[168,621,290],{"class":289},[168,623,262],{"class":196},[168,625,627,629,631,633,635,637,639,641],{"class":626,"line":162},[170,228],[168,628,112],{"class":231},[168,630,234],{"class":196},[168,632,246],{"class":211},[168,634,304],{"class":215},[168,636,246],{"class":211},[168,638,249],{"class":196},[168,640,311],{"class":174},[168,642,262],{"class":196},[168,644,645,647,649],{"class":170,"line":316},[168,646,319],{"class":174},[168,648,179],{"class":178},[168,650,186],{"class":174},[63,652,326,653,329,655,333,657,337,659,340],{},[80,654,152],{},[89,656,332],{},[80,658,336],{},[80,660,152],{},[63,662,343,663,346],{},[80,664,112],{},[144,666,349],{"id":116},[63,668,352,669,153],{},[80,670,355],{},[155,672,674],{"className":157,"code":358,"filename":359,"highlights":673,"language":163,"meta":164,"style":164},[161,265],[80,675,676,686,704,708,729,750],{"__ignoreMap":164},[168,677,678,680,682,684],{"class":170,"line":171},[168,679,175],{"class":174},[168,681,179],{"class":178},[168,683,183],{"class":182},[168,685,186],{"class":174},[168,687,688,690,692,694,696,698,700,702],{"class":170,"line":189},[168,689,193],{"class":192},[168,691,197],{"class":196},[168,693,381],{"class":200},[168,695,204],{"class":196},[168,697,208],{"class":207},[168,699,212],{"class":211},[168,701,163],{"class":215},[168,703,218],{"class":211},[168,705,706],{"class":170,"line":221},[168,707,224],{"emptyLinePlaceholder":11},[168,709,711,713,715,717,719,721,723,725,727],{"class":710,"line":161},[170,228],[168,712,274],{"class":273},[168,714,403],{"class":277},[168,716,281],{"class":273},[168,718,381],{"class":231},[168,720,234],{"class":196},[168,722,246],{"class":211},[168,724,243],{"class":215},[168,726,246],{"class":211},[168,728,262],{"class":196},[168,730,732,734,736,738,740,742,744,746,748],{"class":731,"line":265},[170,228],[168,733,274],{"class":273},[168,735,278],{"class":277},[168,737,281],{"class":273},[168,739,381],{"class":231},[168,741,234],{"class":196},[168,743,246],{"class":211},[168,745,304],{"class":215},[168,747,246],{"class":211},[168,749,262],{"class":196},[168,751,752,754,756],{"class":170,"line":270},[168,753,319],{"class":174},[168,755,179],{"class":178},[168,757,186],{"class":174},[63,759,449,760,103],{},[97,761,454],{"href":452,"rel":762},[101],[456,764,458],{},{"title":164,"searchDepth":189,"depth":189,"links":766},[767,768],{"id":112,"depth":189,"text":146},{"id":116,"depth":189,"text":349},{"categories":770,"date":465,"readingTime":771},[179],{"text":472,"minutes":473,"time":474,"words":475},{"title":54,"description":466},{"loc":470,"images":774,"lastmod":465},[775,776],{"loc":75},{"loc":123},"UvMJv9fDTTWsHv3wXE6BlGpRcvh39kB0HBBcd84oOEY",[779,783],{"title":780,"path":781,"stem":782,"children":-1},"Vue Tip: Force-Enable Vue Devtools in Production Build","\u002Fvue-tips\u002Fforce-enable-vue-devtools-in-production-build","vue-tips\u002Fforce-enable-vue-devtools-in-production-build\u002Findex",{"title":784,"path":785,"stem":786,"children":-1},"Vue Tip: Change the Interpolation Delimiter","\u002Fvue-tips\u002Fchange-the-interpolation-delimiter","vue-tips\u002Fchange-the-interpolation-delimiter\u002Findex"]