Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks入门 #1

Open
urnwangss opened this issue Jan 28, 2021 · 0 comments
Open

Hooks入门 #1

urnwangss opened this issue Jan 28, 2021 · 0 comments

Comments

@urnwangss
Copy link
Owner

urnwangss commented Jan 28, 2021

一、设计动机
1、组件之前复用状态逻辑很难
2、复杂组件变得难以理解
3、南里理解的Class

二、相关使用
1、useState
// useState 是最简单的一个 hook
// 唯一需要注意的是不要尝试在循环或条件等不稳定的代码结构中编写
// 原因在这里 -> brickspert/blog#26

      import React from 'react';
      const { useState } = React;

      export default function UseState() {
        const [num1, setNum1] = useState(0);
        const [num2, setNum2] = useState(0);
        console.warn('render');
        return (
          <div>
            useState Demo
            <h4>num1:{num1}</h4>
            <h4>num2:{num2}</h4>
            <button onClick={() => setNum1(num1 + 1)}>add num1</button>
            <button onClick={() => setNum2(n => n + 1)}>add num2</button>
          </div>
        );
      }
   

    2、useEffect
       // useState 是最简单的一个 hook
      // 唯一需要注意的是不要尝试在循环或条件等不稳定的代码结构中编写
      // 原因在这里 -> https://github.com/brickspert/blog/issues/26

      import React from 'react';
      const { useState } = React;

      export default function UseState() {
        const [num1, setNum1] = useState(0);
        const [num2, setNum2] = useState(0);
        console.warn('render');
        return (
          <div>
            useState Demo
            <h4>num1:{num1}</h4>
            <h4>num2:{num2}</h4>
            <button onClick={() => setNum1(num1 + 1)}>add num1</button>
            <button onClick={() => setNum2(n => n + 1)}>add num2</button>
          </div>
        );
      }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant