React CreateContext Hook in action

by Admin 2. January 2020 05:56

Index.tsx

import React from 'React'

const State = React.CreateContext

export function Parent(props ){

const obj = { text : 'sample text here'};

return <Store.Provider value = {obj}>{props.children}</Store.Provider>

}

export function Child(){

const hook = React.useContext(State);

return <div>{hook.text}</div>

}

 

App.tsx

import {Parent, Child} from './index'

now the app will be rendered as child from parent as below

<ReactDom.Render><Parent><Child /></Parent></ReactDom.Render>

 

Tags:

Comments are closed

TextBox

Tag cloud

    RecentPosts