Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

set type for usecontext

const mycontext = createContext<myType>('default value')
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

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

useContext

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

PREVIOUS NEXT
Code Example
Typescript :: react-native-typescript issue 
Typescript :: get key value typescript 
Typescript :: downloading youtube playlists using youtube-dl in highest quality 
Typescript :: arrow function in typescript 
Typescript :: Cannot show Automatic Strong Passwords for app bundleID: com.williamyeung.gameofchats due to error: iCloud Keychain is disabled 
Typescript :: bullets in latex with header 
Typescript :: how to put column value counts into a histogram 
Typescript :: event type typescript angular 
Typescript :: Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser. 
Typescript :: extend typescript 
Typescript :: create custom objects for user in firebase 
Typescript :: how to check if key exists in json object c# 
Typescript :: what are the common mistakes in testing 
Typescript :: get n random elements from list java 
Typescript :: plot multiple plots in r 
Typescript :: redux persist typescript 
Typescript :: how to compare two lists element by element in python and return matched element 
Typescript :: types of variables typescript 
Typescript :: c# check list of objects for value 
Typescript :: spyon observable 
Typescript :: types for array props 
Typescript :: how to read temp file in windowsnodejs 
Typescript :: beziere curve function 
Typescript :: View and navigate your assignments (teacher) code for asp.net 
Typescript :: mat card api 
Typescript :: i comparer for lists c# 
Typescript :: how to search for elements that are on the webpage using html 
Typescript :: jest not tocontain 
Typescript :: How to disable form control but keep value 
Typescript :: get number of digits in an integer python without converting to string 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =