React开发人员面临的三大编码挑战

2025-05-29 0 16

【51CTO.com快译】React 库很大并且有很多概念,每个 React项目都需要专家级别的开发人员。React专家不仅要熟悉React相关概念,还应该知道如何在实时项目中使用它们。

React开发人员面临的三大编码挑战

但是招聘到专家级别的React开发人员并不容易。因为只有经验丰富的开发人员才能解决开发中的编码挑战,例如高级的概念。在本文,将为你列出 React 专家面临的三大编码挑战。

创建高阶组件以重用组件逻辑

高阶组件是开发人员用于重用组件逻辑的高级技术。重用代码是 React 专家应该具备的一项重要技能。具有可重用性的主要原因是代码优化。

在此编码挑战中,你可能会被要求创建三个具有相似组件逻辑的不同组件。因此,你必须创建一个具有组件逻辑的高阶组件,并且它将被其他三个组件重用。

对于这个挑战,你有三个组件,每个组件都包含一个按钮,该按钮将状态中的值增加一个特定的数字。假设,三个组件是:

  • “ComponentA”,其中按钮将值增加 2。
  • “ComponentB”,其中按钮将值增加 20。
  • “ComponentC”,其中按钮将值增加 200。

首先,用逻辑创建一个 HOC。

  1. import{useState}from"react";
  2. constHigherOrderComponent=(Component,incrementValue)=>{
  3. constHOCFun=()=>{
  4. const[value,setValue]=useState(0);
  5. constincrementHandler=()=>{
  6. setValue(value+incrementValue);
  7. };
  8. return<Componentvalue={value}incrementHandler={incrementHandler}/>;
  9. };
  10. returnHOCFun;
  11. };
  12. exportdefaultHigherOrderComponent;

“HigherOrderComponent”有两个参数——一个组件和状态将增加的数字。然后,创建一个具有组件逻辑的函数。该逻辑包含一个状态变量,其值由处理程序使用传入数字递增。

这个函数使用 props – value 和 incrementHandler 返回传入的组件。请记住,这是使用 HOC 制作的新组件。最后,这个函数会被返回,因为它将在现有组件中使用。

现在,让我们在“ComponentA”、“ComponentB”和“ComponentC”中使用 HOC。

组件A:

  1. importHigherOrderComponentfrom"./HigherOrderComponent";
  2. constComponentA=({value,incrementHandler})=>{
  3. return(
  4. <div>
  5. <buttononClick={incrementHandler}>Incrementby2</button>
  6. <h2>{value}</h2>
  7. </div>
  8. );
  9. };
  10. exportdefaultHigherOrderComponent(ComponentA,2);

组件B:

  1. importHigherOrderComponentfrom"./HigherOrderComponent";
  2. constComponentB=({value,incrementHandler})=>{
  3. return(
  4. <div>
  5. <buttononClick={incrementHandler}>Incrementby29</button>
  6. <h2>{value}</h2>
  7. </div>
  8. );
  9. };
  10. exportdefaultHigherOrderComponent(ComponentB,20);

组件C:

  1. importHigherOrderComponentfrom"./HigherOrderComponent";
  2. constComponentC=({value,incrementHandler})=>{
  3. return(
  4. <div>
  5. <buttononClick={incrementHandler}>Incrementby200</button>
  6. <h2>{value}</h2>
  7. </div>
  8. );
  9. };
  10. exportdefaultHigherOrderComponent(ComponentC,200);

这些组件都不包含任何逻辑,但一切正常。

发生这种情况是因为使用高阶组件来实现代码可重用性。

现在,请记住,此编码挑战的动机是检查你如何创建高阶组件并重用逻辑。

实现和使用 Redux

随着应用程序的增长,管理全局状态变得困难。Redux 是最流行的第三方库,用于通过 React 进行状态管理。专业的 React 开发人员应该了解 Redux 是什么以及它是如何工作的。所以面试可以要求你在一个基本的 React 应用程序中实现 Redux。

在这个编码挑战中,面试官想检查你是如何实现和使用 Redux 的。因此,你可能会获得一个包含两个组件的基本 React 应用程序——一个包含用于增加和减少全局状态的按钮,另一个包含用于显示值的按钮。

首先,创建减速器。

  1. exportconstreducer=(state={value:0},action)=>{
  2. switch(action.type){
  3. case"INCREMENT_VALUE":
  4. return{
  5. …state,
  6. value:action.payload+1,
  7. };
  8. case"DECREMENT_VALUE":
  9. return{
  10. …state,
  11. value:action.payload-1,
  12. };
  13. default:
  14. return{…state};
  15. }
  16. };

除了类型之外,reducer 还会从动作中接收有效载荷。

然后,创建动作创建者。你也可以创建普通动作,但创建动作创建者表明你使用过复杂的 Redux。

  1. exportconstincrementValueAction=(value)=>{
  2. return{
  3. type:"INCREMENT_VALUE",
  4. payload:value,
  5. };
  6. };
  7. exportconstdecrementValueAction=(value)=>{
  8. return{
  9. type:"DECREMENT_VALUE",
  10. payload:value,
  11. };
  12. };

接下来,创建商店。

  1. import{createStore}from"redux";
  2. import{reducer}from"./Reducers/reducers";
  3. constinitialState={
  4. value:0,
  5. };
  6. conststore=createStore(reducer,initialState);
  7. exportdefaultstore;

最后,使用 Provider 为商店包装应用程序。

  1. import{Provider}from"react-redux";
  2. importstorefrom"./store";
  3. importComponent1from"./Components/Component1";
  4. importComponent2from"./Components/Component2";
  5. functionApp(){
  6. return(
  7. <Providerstore={store}>
  8. <divclassName="App">
  9. <Component1/>
  10. <hr/>
  11. <Component2/>
  12. </div>
  13. </Provider>
  14. );
  15. }
  16. exportdefaultApp;

上半场准备好了。Redux 已实现,但作业尚未完成,因为在 React 组件中使用它仍然未决。为此,我们将使用 react-redux 钩子。请记住,不要使用旧的 connect() 函数。

首先,安装“react-redux”,然后使用组件中的 useDispatch 和 useSelector react-redux 钩子。

组件 1:

  1. import{useDispatch,useSelector}from"react-redux";
  2. import{
  3. decrementValueAction,
  4. incrementValueAction,
  5. }from"../ActionCreators/actionCreators";
  6. constComponent1=()=>{
  7. constdispatch=useDispatch();
  8. constvalue=useSelector((state)=>state.value);
  9. console.log(value);
  10. constincrementHandler=()=>{
  11. dispatch(incrementValueAction(value));
  12. };
  13. constdecrementHandler=()=>{
  14. dispatch(decrementValueAction(value));
  15. };
  16. return(
  17. <div>
  18. <buttononClick={incrementHandler}>Increment</button>
  19. <buttononClick={decrementHandler}>Decrement</button>
  20. </div>
  21. );
  22. };
  23. exportdefaultComponent1;

组件2:

  1. import{useSelector}from"react-redux";
  2. constComponent2=()=>{
  3. constvalue=useSelector((state)=>state.value);
  4. return(
  5. <div>
  6. <h2>{value}</h2>
  7. <hr/>
  8. </div>
  9. );
  10. };
  11. exportdefaultComponent2;

使用 react-redux hooks,按钮将起作用。

现在,主要动机是检查你的 redux 知识。面试可能会要求你在其中使用 redux-thunk,从而使这个挑战变得更加困难。此外,使用 react-redux 钩子可以给人更好的印象并避免使用旧技术。

在不使用 props 的情况下在组件之间共享数据

在这个编码挑战中,面试可能会给你一个带有多个嵌套组件的 React 应用程序,如下所示。

React开发人员面临的三大编码挑战

组件“B”是“A”的子组件,而组件“C”和“D”是“B”的子组件。

假设组件“A”中有一个对象,并且在“C”和“D”中需要它。有两种方法可以在不使用 props 的情况下在这些嵌套组件中共享此对象。第一种是使用 Redux。但是在面试官想要避免使用 props 的情况下,永远不要使用 Redux,因为 Redux 是为复杂的项目设计的。实际上,面试官期待这个编码挑战的“前提场景”。

对于这个挑战,首先,创建一个场景应用。

  1. importReactfrom"react";
  2. constDemoContext=React.createContext();
  3. exportdefaultDemoContext;

然后,使用此场景,将组件树包装在 Provider 中。

  1. importDemoContextfrom"../DemoContext";
  2. importBfrom"./B";
  3. constA=()=>{
  4. constobj={
  5. a:1,
  6. b:2,
  7. c:3,
  8. };
  9. return(
  10. <DemoContext.Providervalue={{obj}}>
  11. <div>
  12. <B/>
  13. </div>
  14. </DemoContext.Provider>
  15. );
  16. };
  17. exportdefaultA;

现在,我们可以访问组件“C”和“D”中的“obj”。有两种使用场景的方法 – 使用 Consumer 和 useContext hook。更喜欢使用 useContext hook,因为它是现代更好的方法。

C:

  1. importReact,{useContext}from"react";
  2. importDemoContextfrom"../DemoContext";
  3. constC=()=>{
  4. const{obj}=useContext(DemoContext);
  5. const{a,b,c}=obj;
  6. return(
  7. <div>
  8. <h2>ComponentC</h2>
  9. <h3>{a}</h3>
  10. <h3>{b}</h3>
  11. <h3>{c}</h3>
  12. </div>
  13. );
  14. };
  15. exportdefaultC;

D:

  1. importReact,{useContext}from"react";
  2. importDemoContextfrom"../DemoContext";
  3. constD=()=>{
  4. const{obj}=useContext(DemoContext);
  5. const{a,b,c}=obj;
  6. return(
  7. <div>
  8. <h2>ComponentD</h2>
  9. <h3>{a}</h3>
  10. <h3>{b}</h3>
  11. <h3>{c}</h3>
  12. </div>
  13. );
  14. };
  15. exportdefaultD;

让我们检查输出。

它不使用道具就可以工作!

对于专业的React开发人员来说,编码挑战可能会很困难。面试官想要检查你对React的了解以及你的工作经验。因此,挑战将有一些高级概念,如HOC、Redux和场景应用。

原文地址:https://developer.51cto.com/art/202111/693075.htm#topx

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 React开发人员面临的三大编码挑战 https://www.kuaiidc.com/90746.html

相关文章

发表评论
暂无评论