zoukankan      html  css  js  c++  java
  • react hook 练手

    代码:App.js

    import React, { useState, useEffect, createContext, useContext, useRef } from 'react';
    import { Button } from 'antd-mobile';
    
    const Text = createContext(); // useContext 要和 createContext 一起使用
    
    export default function App() {
      const inputRef = useRef(); // 操作DOM
    
      const [ count, setCount ] = useState(0);
      const [ name, setName ] = useState('张三');
    
      useEffect(() => {
        console.log('---------------componentDidMount-------------', count);
        return () => {
          console.log('---------------componentWillUnmount-------------');
        }
      },[count]) // componentDidUpdate
    
      const handleClick = () => {
        inputRef.current.focus();
      }
    
      return (
        <div className="App">
          <Button onClick={() => setCount(count + 1)}>+1</Button>
          <div>{count}</div>
          <Button onClick={() => setCount(count - 1)}>-1</Button>
          <div>{name}</div>
          <Button onClick={() => setName('张三')}>张三</Button>
          <Button onClick={() => setName('李四')}>李四</Button>
          
          <Text.Provider value={{ count, setCount }}>
            <Item />
          </Text.Provider>
    
          <input type="text" ref={inputRef} />
          <br />
          <Button onClick={handleClick}>Click</Button>
        </div>
      );
    }
    
    function Item () {
      const { count, setCount } = useContext(Text);
      return (
        <div>
          Item_count:{count}
          <br />
          <Button onClick={() => setCount(count - 1)}>-1</Button>
        </div>
      );
    }

    .

  • 相关阅读:
    iOS去除导航栏和tabbar的横线
    各种坑
    iOS系统消息
    文件的读写
    MAC机中安装ruby环境--转载
    一句话处理服务器头像的尺寸
    开一个线程来处理 耗时的操作
    angular2中一种换肤实现方案
    一句话说明==和equals的区别
    下拉框样式在不同浏览器的简单兼容
  • 原文地址:https://www.cnblogs.com/crazycode2/p/12194772.html
Copyright © 2011-2022 走看看