事件与函数组件

huangapple go评论73阅读模式
英文:

Event with function components

问题

I have just started learning REACT and has taken some courses online. I have created a very simple function component, and I want to implement an event, that the parent components can consume. I can find a lot of samples and articles on the net on how to do this using bind (int the constructor) with class components. However I have not found any articles on function components. Anyone has a simple sample or article on how to do this? In general I see many recommendations on using function components over class components, but much more articles and frameworks where people are using class components. Are function components fairly new?

import React, { useState } from "react";
import {
Dropdown,
DropdownToggle,
DropdownMenu,
DropdownItem,
Button
} from "reactstrap";

const DropdownPaging = props => {
const [selectedValue, setSelectedValue] = useState(10);
const [dropdownOpen, setDropdownOpen] = useState(false);

const toggle = () => setDropdownOpen(prevState => !prevState);

function dropDownChanged(val) {
setSelectedValue(val);
}

return (



{selectedValue}


<DropdownItem onClick={() => dropDownChanged(10)}>
10

<DropdownItem onClick={() => dropDownChanged(25)}>
25

<DropdownItem onClick={() => dropDownChanged(50)}>
50

<DropdownItem onClick={() => dropDownChanged(100)}>
100


);
};

export default DropdownPaging;

英文:

I have just started learning REACT and has taken some courses online. I have created a very simple function component, and I want to implement an event, that the parent components can consume. I can find a lot of samples and articles on the net on how to do this using bind (int the constructor) with class components. However I have not found any articles on function components. Anyone has a simple sample or article on how to do this? In general I see many recommendations on using function components over class components, but much more articles and frameworks where people are using class components. Are function components fairly new?

import React, { useState } from &quot;react&quot;;
import {
  Dropdown,
  DropdownToggle,
  DropdownMenu,
  DropdownItem,
  Button
} from &quot;reactstrap&quot;;

const DropdownPaging = props =&gt; {
  const [selectedValue, setSelectedValue] = useState(10);
  const [dropdownOpen, setDropdownOpen] = useState(false);

  const toggle = () =&gt; setDropdownOpen(prevState =&gt; !prevState);

  function dropDownChanged(val) {
    setSelectedValue(val);
  }

  return (
    &lt;div&gt;
      &lt;table&gt;
        &lt;tr&gt;
          &lt;td&gt;
            &lt;Button outline color=&quot;dark&quot;&gt;
              &amp;lt;
            &lt;/Button&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;Button outline color=&quot;dark&quot;&gt;
              &amp;gt;
            &lt;/Button&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;Dropdown isOpen={dropdownOpen} toggle={toggle}&gt;
              &lt;DropdownToggle caret outline color=&quot;dark&quot;&gt;
                {selectedValue}
              &lt;/DropdownToggle&gt;
              &lt;DropdownMenu&gt;
                &lt;DropdownItem onClick={() =&gt; dropDownChanged(10)}&gt;
                  10
                &lt;/DropdownItem&gt;
                &lt;DropdownItem onClick={() =&gt; dropDownChanged(25)}&gt;
                  25
                &lt;/DropdownItem&gt;
                &lt;DropdownItem onClick={() =&gt; dropDownChanged(50)}&gt;
                  50
                &lt;/DropdownItem&gt;
                &lt;DropdownItem onClick={() =&gt; dropDownChanged(100)}&gt;
                  100
                &lt;/DropdownItem&gt;
              &lt;/DropdownMenu&gt;
            &lt;/Dropdown&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
    &lt;/div&gt;
  );
};

export default DropdownPaging;

答案1

得分: 1

功能性组件早已存在,其中一个新功能是处理状态和生命周期方法。

所以在16.8版本之前,功能性组件纯粹用于渲染呈现部分,因此无法处理状态或处理生命周期方法。

因此,当处理大型应用程序时,问题在于如果需要有一个单一的状态变量,您需要将功能性组件更改为类组件。

因此,在16.8版本发布后,React团队引入了钩子,现在您可以使用钩子来处理状态和生命周期方法。

它是如何演变成钩子的基本上

  1. 渲染属性 - 在组件之间共享代码 - 此处
  2. 高阶组件 - 重用组件逻辑 - 此处

您可以在此处了解更多关于钩子的信息。

使用钩子的示例

  1. 异步数据加载 - 此处
  2. 使用状态 - 此处
  3. 渲染表格数据并根据状态键禁用行 - 此处
  4. 自定义钩子 - 窗口调整大小 - 此处

如果您正在更新和尝试以上代码,请分叉它并进行更新。(这对其他人会很有帮助:)

英文:

No, functional components are there from a long back, One of the feature that functional components got new is handling the state and life cycle method

So before the version of 16.8, functional components were purely used to render the presentational part, so it can't handle state or handle life cycle methods

So when dealing with big applications the problem was if you need to have a single state variable you need to change the functional components to class components

So then after the release of 16.8 react team introduced hooks, so now you can handle state and life cycle methods with the help of hooks

How it came to hooks is basically

  1. Render Props - Sharing Code between components - here
  2. Hoc Components - Reusing component logic - here

You can read more on here about hooks.

Example of using hooks

  1. Async data loading - here
  2. Using state - here
  3. Rendering table data and disabling the row based on state key - here
  4. Custom Hook - window resize - here

If you are updating and trying above codesandbox fork it and update. (It will be helpful for others 事件与函数组件 )

huangapple
  • 本文由 发表于 2020年1月6日 17:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定