DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Custom React Hooks to Simplify Complex Scenarios
  • The Power of @ngrx/signalstore: A Deep Dive Into Task Management
  • Unleashing the Power of React Hooks
  • Utilizing Database Hooks Like a Pro in Node.js

Trending

  • AWS to Azure Migration: A Cloudy Journey of Challenges and Triumphs
  • *You* Can Shape Trend Reports: Join DZone's Software Supply Chain Security Research
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  • Mastering Fluent Bit: Installing and Configuring Fluent Bit on Kubernetes (Part 3)
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. ̶R̶e̶a̶c̶t̶ VUE Context Provider Pattern With Custom Hooks

̶R̶e̶a̶c̶t̶ VUE Context Provider Pattern With Custom Hooks

Learn how to use React context provider pattern with Vue Composition API. Find out the benefits and advantages of this pattern using code examples inside the sandbox.

By 
Eugene Sincovschi user avatar
Eugene Sincovschi
·
Nov. 17, 21 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
2.9K Views

Join the DZone community and get the full member experience.

Join For Free

The Problem

Vue has lots of tools out of the box that makes our life easier, e.g Vuex and Vue-router. But, it still relies too much on magical connections, which makes code hard to understand. In order to help IDE and future me with code readability, I've started to think about hooks and patterns that come along with them.

The Code

In this sandbox, you can find React's Context Provider Pattern applied to Vue's Composition API provide and inject methods.

The Conclusion

Vue has a very powerful plugin Vuex for state management. But, often, you are unable to share the state in a simpler way or to multiply the same state functionality. Therefore, you have two standard solutions, props drilling and provide/inject. Drilling is good as long it's no deeper than 3 levels. But provide/inject, in their raw form, are relying too much on magic for my taste.

An important note: This pattern is not intended to replace Vuex, but to complement it. The rule of thumb here is to have the Vuex store for the global state and the Context state for a group of components. For E.g, Vuex is used to bring userData inside the Header Component, closer to the User Menu. And the User Menu Root Component is passing data from Store to its atomic components via Context Provider pattern using under the hood provide and inject.

This gives us Decoupled Components that are easy to run and test separately with Storybook, Mocks, Jest Snapshot, etc.

The Takeaway

If Composition API borrows so much from React already, why not keep that in mind and make Vue more lovable by combining the best of both worlds? Don't be afraid to experiment by mixing features and patterns for the greater good.

Hook

Published at DZone with permission of Eugene Sincovschi. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Using Custom React Hooks to Simplify Complex Scenarios
  • The Power of @ngrx/signalstore: A Deep Dive Into Task Management
  • Unleashing the Power of React Hooks
  • Utilizing Database Hooks Like a Pro in Node.js

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!