zoukankan      html  css  js  c++  java
  • xml ui

    useContext saves you the stress of having to rely on a Context consumer.

    const contextValue = useContext(contextObject)
    
    // consuming context via a consumer: 
    const ThemeContext = React.createContext("dark");
    
    function Button() {
        return <ThemeContext.Consumer>
            {theme => <button className={theme}>Amazing button</button>}
        </ThemeContext.Consumer>
    }
    
    const ThemeContext = React.createContext('light')
    
    const Display = () => {
      return (
        <ThemeContext.Consumer>
          {theme => (
            <div
              style={{
                background: theme === 'dark' ? 'black' : 'papayawhip',
                color: theme === 'dark' ? 'white' : 'palevioletred',
                 '100%',
                minHeight: '200px'
              }}
            >
              {'The theme here is ' + theme}
            </div>
          )}
        </ThemeContext.Consumer>
      )
    }
    render(Display)
    
    // consume context with the useContext hook: 
    import {useContext} from 'react';
    
    function ButtonHooks() {
      const theme = useContext(ThemeContext)
      return <button className={theme}>Amazing button</button>
    }
    
    const ThemeContext = React.createContext('light');
    
    const Display = () => {
     const theme = useContext(ThemeContext);
     return <div
            style={{
            background: theme === 'dark' ? 'black' : 'papayawhip',
            color: theme === 'dark' ? 'white' : 'palevioletred',
             '100%',
            minHeight: '200px'
            }}
        >
            {'The theme here is ' + theme}
        </div>
    }
    render(Display)
    

    https://react-hooks-cheatsheet.com/

  • 相关阅读:
    c++之类模板
    c++之函数模板
    c++之继承三
    c++之继承二
    c++之继承一
    c++之类类型转换
    c++之运算符重载二
    c++之运算符重载一
    Mahout学习路线路
    数据库分区
  • 原文地址:https://www.cnblogs.com/Searchor/p/13877931.html
Copyright © 2011-2022 走看看