Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

React useContext

import React from 'react';

export const UserContext = React.createContext();

export default function App() {
  return (
    <UserContext.Provider value="Reed">
      <User />
    </UserContext.Provider>
  )
}

function User() {
  const value = React.useContext(UserContext);  
    
  return <h1>{value}</h1>;
}
Comment

usecontext react

import React, { useContext, createContext } from 'react'

const ThemeContext = React.createContext()

function Title() {
  const theme = useContext(ThemeContext)

  const style = {
    background: theme.primary,
    color: theme.text,
  }

  return <h1 style={style}>Title</h1>
}

function Nested() {
  return <Title />
}

function NestedTwice() {
  return <Nested />
}

export default function App() {
  const theme = {
    primary: 'dodgerblue',
    text: 'white',
  }

  return (
    <ThemeContext.Provider value={theme}>
      <NestedTwice />
    </ThemeContext.Provider>
  )
}
Comment

react usecontext examples

import React from "react";
import ReactDOM from "react-dom";

// Create a Context
const NumberContext = React.createContext();
// It returns an object with 2 values:
// { Provider, Consumer }

function App() {
  // Use the Provider to make a value available to all
  // children and grandchildren
  return (
    <NumberContext.Provider value={42}>
      <div>
        <Display />
      </div>
    </NumberContext.Provider>
  );
}

function Display() {
  // Use the Consumer to grab the value from context
  // Notice this component didn't get any props!
  return (
    <NumberContext.Consumer>
      {value => <div>The answer is {value}.</div>}
    </NumberContext.Consumer>
  );
}

ReactDOM.render(<App />, document.querySelector("#root"));



//now with useContext the same page look like this
// import useContext (or we could write React.useContext)
import React, { useContext } from 'react';

// ...

function Display() {
  const value = useContext(NumberContext);
  return <div>The answer is {value}.</div>;
}
Comment

React useContext

import React, { createContext } from 'react'

export const Context = createContext()

export const Provider = ({ children }) => {
  const [state, setState] = useState({})

  const value = {
    state,
    setState,
  }

  return <Context.Provider value={value}>{children}</Context.Provider>
}
Comment

useContext

import { createContext } from 'react';
const Context = createContext('Default Value');
Comment

can we use usecontext in class component

import { Context } from '../context/ChatListContext'

class MainScreen extends Component {

 static contextType = Context

  constructor(props) {
    super(props)
    this.state = { loading: true, showAction: false }
    setTimeout(() => {
      StatusBar.setBackgroundColor(primary)
    }, 100)
  }
...
  render() {
       const {state, fetchChatList} =this.context;
  }
}
Comment

React useContext

import React from 'react'

import { Provider } from './Context'
import { ConsumingComponent } from './ConsumingComponent'

export const Page = () => {
  return (
    <div>
      <Provider>
        <ConsumingComponent />
      </Provider>
    </div>
  )
}
Comment

React useContext

import React, { useContext } from 'react'

import { Context } from './Context'

export const ConsumingComponent = () => {
  const { state } = useContext(Context)

  return null
}
Comment

useContext

function Main() {
  const value = 'My Context Value';
  return (
    <Context.Provider value={value}>
      <MyComponent />
    </Context.Provider>
  );
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to take 100% width in react native 
Javascript :: delete document mongoose 
Javascript :: jquery select input with empty value 
Javascript :: tolowercase js 
Javascript :: react memo 
Javascript :: NextJS + Material-UI - Warning: Prop className did not match 
Javascript :: expect any function jest 
Javascript :: 1. Write regular expression to describe a languages consist of strings made of even numbers a and b. CO1 K3 
Javascript :: js array 0 to n 
Javascript :: round number to 2 symbols after comma 
Javascript :: react tabs 
Javascript :: jquery disable all forms 
Javascript :: javascript sort associative array 
Javascript :: @apify/http-request 
Javascript :: remove object from array by value javascript 
Javascript :: remove element onclick javascript 
Javascript :: express referrer 
Javascript :: javascript function from string 
Javascript :: angular 9 radio button checked 
Javascript :: label in lwc 
Javascript :: rgb to hex conversion 
Javascript :: get url in javascript 
Javascript :: generate random number js 
Javascript :: comment in javascript 
Javascript :: onpress setstate react native 
Javascript :: nextjs docs 
Javascript :: update map value javascript 
Javascript :: javascript array filter elements greater than 
Javascript :: make object readonly javascript 
Javascript :: volume slider javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =