React Context: How to Use the useContext Hook

In React, the components are the building blocks of the product. These components are defined in the tree hierarchy where one component is the parent of the child component.

The data flow in react is down the hierarchy i.e. the data flow from parent component to child component and further down. If the component is deep down in the hierarchy from the root component then the data is passed through all the middle components first and then it will be accessible to the component that is deep down the hierarchy.

To prevent this type of design some reactjs developers use redux as a library that works on the global store that stores the data or state globally and is accessible to all the components directly without passing data or state down the hierarchy.

Context is another very curious and awesome way introduced by react to store the data or state globally.

For implementing react context in your project:

1. Create a file to create a context object and export that context object from that file.

theme-context.js import React from ‘react’ export const ThemeContext = React.createContext( ‘dark’ );

Here we have created and exported a context object. Context object accepts a default parameter or value ( default props ) that can be passed to it. In case if no props are passed by the parent component then it takes default props defined in the context object.

A react context object always comes with a provider function. In the next step, we will see what a provider is.

2. Now in a parent component whose values are to be passed to child component.

We will wrap any child component that is going down the hierarchy in a react context provider.

Note: If any child component wants to access the state or value from ancestor than the parent or grandparent of that child component should be wrapped inside a react context provider.

Component A —> Component B —-> Component C ——> Component D

(ancestor) (child 1) (child 2) (child 3)

Here Component A is the root component, component B is the child of component A, component C is the child of component B, component D is the child of component C.

If child 3 wants to access the data from the ancestor or component A than any of component B, C or D should be wrapped in provider.

The Child who wants to access the data, its any parent, or itself should be wrapped inside the context provider of the ancestor that pass the data.

app.js import {ThemeContext, themes} from ‘./theme-context’; import ThemedButton from ‘./themed-button’; // An intermediate component that uses the ThemedButton function Toolbar(props) { return ( Change Theme ThemedButton> ); } class App extends React.Component { constructor(props) { super(props); this.state = { theme: themes.light, }; this.toggleTheme = () => { this.setState(state => ({ theme: state.theme === themes.dark ? themes.light : themes.dark, })); }; } render() { // The ThemedButton button inside the ThemeProvider // uses the theme from the state while the one outside uses // the default dark theme return ( Section> Page> ); } } ReactDOM.render(, document.root);

Here class App is the ancestor. The Toolbar is a child functional component and ThemedButton is also a child component.

The Toolbar is wrapped inside the context provider. The context provider passes the values that can be accessible to Toolbar and its any child component. ThemedButton is not wrapped inside any of the context providers, therefore, it will take the default value of the context.

The value passed by the provider to the child component can be accessible by it or any child of that child component.

Note: Any child component wrapped inside the provider will take the values passed and if the child component is not wrapped inside the provider it will use the default value of the react context provider.

So we are done with parent component, now let’s move on to the child component definition.

3. In child components, we can access the values in multiple ways.

Let’s discuss the first way to access the value in child component (contextType).

Themed-button.js Before Context import {ThemeContext} from ‘./theme-context’; class ThemedButton extends React.Component { render() { let props = this.props; let theme = this.props; // this.props used return ( ); } } ThemedButton.contextType = ThemeContext; export default ThemedButton; After Context import {ThemeContext} from ‘./theme-context’; import React from ‘react’ class ThemedButton extends React.Component { render() { let props = this.props; let theme = this.context; //Replaced by this.context return ( ); } } ThemedButton.contextType = ThemeContext; export default ThemedButton;

Here ThemedButton is the child component. If you want to access the value then make use contextType before exporting the component. After that everywhere you have used this.props can be replaced by this.context.

Another way of accessing values is by using react hooks.

import {ThemeContext} from ‘./theme-context’; import React, {useContext} from ‘react’ class ThemedButton extends React.Component { render() { let theme = useContext(ThemeContext); return ( ); } } export default ThemedButton;

Here we made use of hooks useContext. After this, we can directly access the properties as shown above.

Be it a software developer, programmer, coder, or a consultant, CronJ has it all. CronJ has been a trustworthy company for startups, small companies, and large enterprises. Hire the web of experienced React developers for your esteemed project today. ReactJS Development Services

How To Boost Immunity Power And Improve Resistance To Infections?

Either knowingly or without your knowledge, you might have taken many foods that are capable enough to improve your immunity health. The thing to remember here is that the healthy functioning of the immune system is something that is important for the humans to stay protected against many illnesses. The immune system is actually compared to that of a soldier that protects the nation. This system protects the humans from external stressors and infections. It fights against a wide range of viruses, pollutants, toxins and bacteria on a daily basis to help us stay healthy and to lead a disease-free life. To improve resistance to infections, this system should be strengthened. How is this possible? Let us find out here:

With Imutol capsules:

It is possible to get the safest answer to the question ‘how to boost immunity power’ with the help of Imutol capsules. These capsules are natural immunity boosting supplements with the combination of the best herbs. Apart from certain herbs added as ingredients in these capsules, processed decoction of some herbs are also added and let us get into the details of the herbs present as ingredients to improve resistance to infections:

Daruharidra: It is particularly known for its efficiency in addressing allergies. It is known to address problems like urinary issues. According to ayurvedic texts, this herb is the best known for its ability to support the immune system.

Haridra: It is identified as a super spice as it possesses a wide range of properties to improve human health. It is known for its cardiovascular properties, antioxidant and anti-inflammatory actions. In addition, it is an anti-cancer herb and it is also an antibiotic. Furthermore, it can improve liver health and it has digestive and energy-clearing properties. With a wide range of benefits and properties, this spice can provide an excellent answer to the question ‘how to boost immunity power’.

Neem: This herb is known for its natural healing properties and it has immeasurable medicinal properties. It is known to boost health, and it destroys skin diseases. As it is anti-infective, it can strengthen the immune system by getting rid of microorganisms that affect the healthy functioning of immune system. It can also prevent infections and this is why it is added as an important ingredient in Imutol capsules to improve resistance to infections.

Kutki: It is identified as a liver tonic as it will help with getting rid of toxic liver and it can improve liver functions. When the liver functions well, all toxins present in the body will be effectively removed from the body by the liver. This, in turn, will contribute towards a healthy immune system to prevent future infection, rather than fighting against toxins that are already present in the body. So, to provide the safe answer to the question ‘how to boost immunity power’, it is added to Imutol capsules.

Chitrak, anatmul, swarna bhasma, tulsi, abhrak bhasma, shatavari and kesar are other herbal ingredients in these capsules to improve resistance to infections. There is decoction of some herbs in these capsules to bring the safe answer to the question how to boost immunity power.

Just Notice the Amazing Benefits of Day Spas in San Diego

Nothing beats a trip to a day spa in San Diego for luxury and relaxation. At Beauty Kliniek, we are dedicated to providing our guests with the most lavish and memorable pampering experiences possible. We have found some fantastic spa treatment San Diego advantages that we hope you will be interested in learning about. It goes without mentioning that going to a spa is an excellent way to unwind and de-stress. A trip to the spa is an excellent way to disconnect from the stresses of daily life. Allowing you this chance to unwind and rest has many advantages, including a calm mind and improved success after the treatment!

Many spa treatments will potentially aid in the prevention of aging. By relaxing skin cells and hydrating the skin, facials are believed to help slow and avoid the emergence of wrinkles. Additionally, taking time to rest and de-stress is a fantastic anti-aging practice in and of itself!
It can be difficult to find time for relaxing daily, but making time for it at San Diego day spa packages can be immensely beneficial. If you have trouble sleeping, one of the many advantages of a spa trip is that many therapies will make you sleep better. Massages fully calm your muscles and reduce your blood pressure, while also assisting you in maintaining a steady heart rhythm, which helps you sleep well.
Exercise, lying on an unsupportive mattress, and working for extended periods at a desk are all factors that contribute to general aches and pains in many people. A soothing massage, either full body or focused on your problem spots, is a perfect way to relieve these aches by thoroughly relaxing the muscle tissue.
When it comes to the connection between spa treatments and weight loss, there are a variety of hypotheses, but many people believe there is one. Hot spa therapies widen the pores of the skin, aid in the removal of chemicals, and enable the body to burn calories. Deep tissue massages can also help the body break down fatty deposits by applying pressure and tension to the skin. Of course, all of these will need to be used in combination with a healthy diet and regular workout to have a significant impact, so it’s good to hear that you’re losing calories and battling cellulite while getting a relaxing massage!
Another great advantage of a body massage is that it helps to maintain blood pressure and increase blood circulation, particularly when done regularly.
Long stretches of standing have been attributed to the development of varicose veins in post-surgical care.
The biological effect of spa treatments on your body, especially in terms of your happiness levels, is one of the many advantages of spa treatments.
Fortunately, one of the many wonderful advantages of attending a spa is that many therapies, such as head and hand massages, can help to reduce the occurrence of headaches.
A facial can help to cleanse and hydrate the skin while also providing the nourishment it requires to appear bright and glowing.