简体   繁体   English

与 React hooks 相关的潜在问题要求面试

[英]Potential issue related to React hooks asked for job interview

So I have a React job interview and have this piece of code in my assignment to close and modal box:所以我有一个 React 工作面试,并在我的关闭和模态框的任务中有这段代码:

const App = (): ReactElement => {

  const [isOpen, setIsOpen] = useState<boolean>(false);

  const renderApp = (): ReactElement => {
    if (isOpen) {
      return <FeedbackForm onClose={() => setIsOpen(false)} />;
    }

    return <button onClick={() => setIsOpen(true)}>Open modal</button>;
  }

  if (isOpen) {
    return <>
      <div className='App'>
        <FeedbackForm onClose={() => setIsOpen(false)} />
      </div>
    </>
  }

  return <>
    <div className='App'>{renderApp()}</div>

  }

export default Feedback;

So here I have an App component that has a Modal component and a simple hook that has a boolean value that indicates if the modal is open or not.所以这里我有一个App组件,它有一个Modal组件和一个简单的钩子,它有一个 boolean 值,指示模态是否打开。 The modal starts off as closed, so the initial value is false .模态以关闭状态开始,因此初始值为false The button to open the modal is separate from the button that has to close the modal.打开模式的按钮与关闭模式的按钮是分开的。 The button to close the modal is within the Modal component while the button to open it is in the App component.关闭 modal 的按钮位于Modal组件中,而打开它的按钮位于App组件中。 Because they're both separate buttons, they can explicitly set the value to either true or false .因为它们都是单独的按钮,所以它们可以显式地将值设置为truefalse

Now, on this topic, I received this question from the code reviewer:现在,关于这个话题,我从代码审查员那里收到了这个问题:

Could you explain the potential issue of the following code?

const [isOpen, setIsOpen] = useState(false)
const toggle = () => setIsOpen(!isOpen);

I can't exactly see the problem in this snippet.我无法在此代码段中完全看到问题。 I've tried running it with a button that calls toggle() and I see it updating the state perfectly fine.我试过用一个调用toggle()的按钮来运行它,我看到它可以很好地更新 state。 What is the potential issue the interviewer is talking about here?面试官在这里谈论的潜在问题是什么?

In the toggle function, we are setting the state based on the current state value.toggle function 中,我们根据当前 state 值设置 state。 React performs updates in batches and it is a possibility that when we run toggle() multiple times, the state is not updated for each subsequent run. React 执行批量更新,当我们多次运行toggle()时,state 可能不会在每次后续运行时更新。 So, to make sure that we don't lose the state updates from the previous runs, we can do something like the following:因此,为了确保我们不会丢失之前运行的 state 更新,我们可以执行以下操作:

const toggle = () => setIsOpen((prevIsOpen) => !prevIsOpen);

Now, in your scenario, it might not happen because we are showing/hiding modal based on toggle click which changes the views completely, but we can't be sure and can't take chances for production deployment.现在,在您的场景中,它可能不会发生,因为我们正在显示/隐藏基于切换单击的模式,这完全改变了视图,但我们不能确定也不能冒险进行生产部署。 If it were something else, like showing/hiding of expansion panel, sidebar, etc. then the issue would be easily visible if you clicked toggle button simultaneously, with minimal delay between clicks.如果是其他东西,例如显示/隐藏扩展面板、侧边栏等,那么如果您同时单击切换按钮,问题将很容易看到,并且单击之间的延迟最小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM