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

set type for usecontext

const mycontext = createContext<myType>('default value')
Comment

react 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 :: javascript reduce function array 
Javascript :: angular async 
Javascript :: vue state 
Javascript :: bootstrap 4 open tab when opening modal 
Javascript :: strapi v4 populate 
Javascript :: Object Property Shorthand javascript 
Javascript :: how to know how many pixels of page be scrolled javascript 
Javascript :: joi validate 
Javascript :: data-toggle="tooltip not working due to jquery-ui.min.js 
Javascript :: angular 7 selected 
Javascript :: bitcoin prices in javascript 
Javascript :: pagination react 
Javascript :: partial filter expression mongodb compass 
Javascript :: add object to array javascript 
Javascript :: vue js filter 
Javascript :: currying javascript 
Javascript :: function if else javascript 
Javascript :: form in react 
Javascript :: in compare method why we taking a and b for sorting in javascript 
Javascript :: passing props in react functional components 
Javascript :: gradle error react native 
Javascript :: como checar valor do input checkbox angular 
Javascript :: req.params 
Javascript :: add marker on map geocoder result mapbox 
Javascript :: bundle 
Javascript :: Enqueuing JavaScript in WordPress 
Javascript :: utc to est javascript 
Javascript :: passing json as datasource to jasper report library 
Javascript :: javascript factory functions 
Javascript :: js new array 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =