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/

  • 相关阅读:
    COM组件
    【游戏引擎架构】入门(一)
    UNICODE字符串
    Python随笔10
    Python随笔9-函数
    Python随笔7
    Python随笔6
    Python随笔5
    Python随笔4
    Python随笔3
  • 原文地址:https://www.cnblogs.com/Searchor/p/13877931.html
Copyright © 2011-2022 走看看