DEV Community

Cover image for Part1- Context Api in React Class Based Component
AKSH DESAI
AKSH DESAI

Posted on • Edited on

2

Part1- Context Api in React Class Based Component

=> Here Folder Structure image here:

Folder Image

Index.js Code:-_

import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));

root.render(
    <React.StrictMode>
        <App />
    </React.StrictMode>
)
Enter fullscreen mode Exit fullscreen mode

App.js Code:-

import React, { Component } from 'react'
import User from './User'
export const myContext = React.createContext();

export default class App extends Component {
    state = {
        name: 'Rahul',
        value: 10
    }

    handleClickContext = () => {
        this.setState({
            value: this.state.value + 1
        })
    }

    render() {
        const contextValue = {
            data: this.state,
            handleClick: this.handleClickContext
        }

        return (
            // <myContext.Provider value={this.state.name}>
            <myContext.Provider value={contextValue}>
                <User />
            </myContext.Provider>
        )
    }
}

Enter fullscreen mode Exit fullscreen mode

User.js Code:

import React, { Component } from 'react'
import Guest from './Guest'

export default class User extends Component {
  render() {
    return (
      <div>
        <h3>User component</h3>
        <Guest />
      </div>
    )
  }
}

Enter fullscreen mode Exit fullscreen mode

Guest.js Code:

import React, { Component } from 'react'
import { myContext } from './App'

export default class Guest extends Component {
    render() {
        return (
            <div>
                <h3>Guest Component</h3>
                <myContext.Consumer>
                    {({ data, handleClick }) =>
                        <>

                            <h4>{data.name}</h4>
                            <h4>{data.value}</h4>

                            <button onClick={handleClick}>Change Value</button>
                        </>
                    }
                </myContext.Consumer>
            </div>
        )
    }
}

Enter fullscreen mode Exit fullscreen mode

Output:-

Output

Thank You.
You can follow us on:
Youtube
Instagram

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay