英文:
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 "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 (
<div>
<table>
<tr>
<td>
<Button outline color="dark">
&lt;
</Button>
</td>
<td>
<Button outline color="dark">
&gt;
</Button>
</td>
<td>
<Dropdown isOpen={dropdownOpen} toggle={toggle}>
<DropdownToggle caret outline color="dark">
{selectedValue}
</DropdownToggle>
<DropdownMenu>
<DropdownItem onClick={() => dropDownChanged(10)}>
10
</DropdownItem>
<DropdownItem onClick={() => dropDownChanged(25)}>
25
</DropdownItem>
<DropdownItem onClick={() => dropDownChanged(50)}>
50
</DropdownItem>
<DropdownItem onClick={() => dropDownChanged(100)}>
100
</DropdownItem>
</DropdownMenu>
</Dropdown>
</td>
</tr>
</table>
</div>
);
};
export default DropdownPaging;
答案1
得分: 1
功能性组件早已存在,其中一个新功能是处理状态和生命周期方法。
所以在16.8版本之前,功能性组件纯粹用于渲染呈现部分,因此无法处理状态或处理生命周期方法。
因此,当处理大型应用程序时,问题在于如果需要有一个单一的状态变量,您需要将功能性组件更改为类组件。
因此,在16.8版本发布后,React团队引入了钩子,现在您可以使用钩子来处理状态和生命周期方法。
它是如何演变成钩子的基本上
您可以在此处了解更多关于钩子的信息。
使用钩子的示例
如果您正在更新和尝试以上代码,请分叉它并进行更新。(这对其他人会很有帮助:)
英文:
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
- Render Props - Sharing Code between components - here
- Hoc Components - Reusing component logic - here
You can read more on here about hooks.
Example of using hooks
- Async data loading - here
- Using state - here
- Rendering table data and disabling the row based on state key - here
- Custom Hook - window resize - here
If you are updating and trying above codesandbox fork it and update. (It will be helpful for others )
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论