admin管理员组文章数量:1404578
I was referring to the below link (section : HOCs for Functional Components)
In the example, below is the code for the HOC;
//functional HOC with useState hook
import React, { useState } from 'react';
function withCountState(Wrapped) {
return function (props) {
const [count, setCount] = useState(0);
props['count'] = count;
props['setCount'] = setCount;
return <Wrapped {...props} />;
}
}
Also, the Wrapped ponent code is as below;
const Wrapped = (props) => {
const {count, setCount} = props;
return(
<div>
<h1>Counter Functional Component</h1>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Increment count
</button>
</div>);
};
For applying HOC to , we use
const EnhancedWrapped = withCountState(Wrapped);
Now I have 2 questions;
- For consuming this ponent, do we just say
<EnhancedWrapped>
may be in our App.js or do we need anything else? - What benefit do we really get out of creating this HOC?
I was referring to the below link (section : HOCs for Functional Components) https://rossbulat.medium./how-to-use-react-higher-order-ponents-c0be6821eb6c
In the example, below is the code for the HOC;
//functional HOC with useState hook
import React, { useState } from 'react';
function withCountState(Wrapped) {
return function (props) {
const [count, setCount] = useState(0);
props['count'] = count;
props['setCount'] = setCount;
return <Wrapped {...props} />;
}
}
Also, the Wrapped ponent code is as below;
const Wrapped = (props) => {
const {count, setCount} = props;
return(
<div>
<h1>Counter Functional Component</h1>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Increment count
</button>
</div>);
};
For applying HOC to , we use
const EnhancedWrapped = withCountState(Wrapped);
Now I have 2 questions;
- For consuming this ponent, do we just say
<EnhancedWrapped>
may be in our App.js or do we need anything else? - What benefit do we really get out of creating this HOC?
2 Answers
Reset to default 3Viet has answered your questions. HOC is a way to make your ponents re-usable through position. You can have other ponents which get wrapped by the HOC and now they would have access to the count and setCount functionality.
Depending upon what you are trying to acplish, it's also a good idea to consider the pitfalls of HOC and consider alternate patterns such as :
- Render Props: React Docs on Render Props
- Using Custom Hooks over HOC Article on custom hooks
When using React Hooks, I'd personally prefer making custom hooks over using HOCs. And depending upon the use case, you may want to check out if React Context would make sense if multiple ponents are going to need a shared state.
For consuming this ponent, do we just say may be in our App.js or do we need anything else? Yes, just use HOC like any other JSX ponent.
What benefit do we really get out of creating this HOC? You can make it reusable. Let's say you want another ponent with different content inside, like , you could just create a new ponent by
const AnotherEnhancedWrapped = withCountState(AnotherWrapped);
本文标签: javascriptUsage of React HOC for Functional ComponentsStack Overflow
版权声明:本文标题:javascript - Usage of React HOC for Functional Components - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744840885a2627916.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论