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

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 :: vue 3 route params 
Javascript :: close alert after 5 seconds javascript 
Javascript :: object js 
Javascript :: args slice discord.js 
Javascript :: Get the index of an Object in an Array in JavaScript 
Javascript :: use svg image in next js 
Javascript :: angular date pipe 
Javascript :: axios get image 
Javascript :: jqeury input checkbox listener not working 
Javascript :: MAC addresses in JavaScript 
Javascript :: export data in json format in javascript 
Javascript :: js settimeout wait element 
Javascript :: react router 6 multiple routes layout 
Javascript :: new Date().toLocaleDateString day 
Javascript :: react hook form validation 
Javascript :: js merge 2 form data 
Javascript :: sequelize array of strings 
Javascript :: rock paper scissors javascript 
Javascript :: regex not something 
Javascript :: set interval 
Javascript :: how to update node js through terminal 
Javascript :: how to export mongodb database to json 
Javascript :: reload datatable without ajax 
Javascript :: going through every attributes of an object javascript 
Javascript :: javascript play audio from buffer 
Javascript :: image file size in react-dropzone 
Javascript :: sequelize mariadb example 
Javascript :: javascript is int 
Javascript :: html select specify deafult select by js variable 
Javascript :: angular capitalize pipe 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =