如何在React中对弹出窗口进行条件渲染。

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

How to perform conditional rendering on a Popup in react

问题

我正在使用React和Tailwind开发网站。我想在React中的弹出组件上进行条件渲染,但我卡住了。以下是这两个页面的设计:

如何在React中对弹出窗口进行条件渲染。

如何在React中对弹出窗口进行条件渲染。

我已经将这两个页面创建为独立的组件,并尝试使用useState来在我的React应用程序中有条件地渲染这两个页面。但由于某种原因,我无法这样做,即使当我将状态初始化为true时,当我控制台记录时,状态为false。

以下是弹出窗口的代码:

import React from 'react';
import { useState } from 'react';
import { MayaLogo, Avatar, Icon } from 'crayon-ui-kit';
import Searchbar from './Searchbar';
import ServicesComponent from './ServicesComponent';
import Reveal from './Reveal';
import './Popup.css';

const SelectSource = (props) => {
  return props.trigger ? (
    <div className="popup" onClick={() => props.setTrigger(false)}>
      {/* ... 省略部分内容 */}
    </div>
  ) : (
    ''
  );
};

const Configure = (props) => {
  return props.trigger ? (
    <div className="popup" onClick={() => props.setTrigger(false)}>
      {/* ... 省略部分内容 */}
    </div>
  ) : (
    ''
  );
};

function Popup(props) {
  const [Flag, setFlag] = useState(true);
  console.log(Flag);
  if (Flag) {
    return (
      <SelectSource
        trigger={props.trigger}
        setTrigger={props.setTrigger}
        SourceSelect={Flag}
        setSourceSelect={setFlag}
      />
    );
  }
  if (!Flag) {
    return (
      <Configure
        trigger={props.trigger}
        setTrigger={props.setTrigger}
        SourceSelect={Flag}
        setSourceSelect={setFlag}
      />
    );
  }
}

export default Popup;

在这里,Reveal只是我创建的一个动画组件。正如我们所看到的,我将Flag的初始状态设置为true,props是用于打开和关闭弹出窗口的触发器和setTrigger状态功能。

然后,我将Flag和setFlag传递给这两个组件,服务组件是按钮图标,如果我们单击按钮,应该能够转到下一页(即第2步配置页面)。但是,当我控制台记录Flag的值时,它显示为未定义。

如何在React中对弹出窗口进行条件渲染。

服务组件的代码如下:

import React from 'react';
import './ServicesComponent.css';

import { companies } from '../constants';
import Image from 'next/image';
const ServicesComponent = (props) => {
  return (
    <div className="servicesList">
      {companies.map((service, index) => {
        return (
          <div className="element_service">
            {/* ... 省略部分内容 */}
          </div>
        );
      })}
      <div className="list"></div>
    </div>
  );
};

export default ServicesComponent;

我不知道我的代码有什么问题。如果有人能提供任何见解,将非常有帮助。

英文:

I am developing a website using React and Tailwind. I want to do conditional rendering on a Popup Component in react and I am stuck, while trying to do so. These are the designs of the two pages:

如何在React中对弹出窗口进行条件渲染。

如何在React中对弹出窗口进行条件渲染。

I have created both these pages as separate components and I tried to use useState to conditionally render both these pages in my react application. But for some reason, I am unable to do so and even when I initialized the state to be true, when I console log i get the State as false.

Here is the Popup code:

import React from &#39;react&#39;;
import { useState } from &#39;react&#39;;
import { MayaLogo, Avatar, Icon } from &#39;crayon-ui-kit&#39;;
import Searchbar from &#39;./Searchbar&#39;;
import ServicesComponent from &#39;./ServicesComponent&#39;;
import Reveal from &#39;./Reveal&#39;;
import &#39;./Popup.css&#39;;

const SelectSource = (props) =&gt; {
  return props.trigger ? (
    &lt;div className=&quot;popup&quot; onClick={() =&gt; props.setTrigger(false)}&gt;
      &lt;div className=&quot;popup-inner&quot; onClick={(e) =&gt; e.stopPropagation()}&gt;
        &lt;div className=&quot;side&quot;&gt;
          &lt;div className=&quot;logo&quot;&gt;
            &lt;MayaLogo name=&quot;HorizontalLogo&quot; size=&quot;112&quot; background=&quot;light&quot; /&gt;
          &lt;/div&gt;
          &lt;div className=&quot;petrol-image&quot;&gt;&lt;/div&gt;
          &lt;div className=&quot;side-text&quot;&gt;
            &lt;Reveal&gt;
              &lt;span className=&quot;grey-text&quot;&gt;Step 1&lt;/span&gt;
            &lt;/Reveal&gt;
            &lt;Reveal&gt;
              &lt;h2 className=&quot;sourcePick&quot;&gt;Pick a Source&lt;/h2&gt;
            &lt;/Reveal&gt;
            &lt;Reveal&gt;
              &lt;p className=&quot;grey-text&quot;&gt;
                You have no connector selected. Please select a source connector
                from our wide range of options to proceed
              &lt;/p&gt;
            &lt;/Reveal&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;button className=&quot;close-btn&quot; onClick={() =&gt; props.setTrigger(false)}&gt;
          &lt;svg
            xmlns=&quot;http://www.w3.org/2000/svg&quot;
            width=&quot;24&quot;
            height=&quot;24&quot;
            viewBox=&quot;0 0 24 24&quot;
            fill=&quot;none&quot;
          &gt;
        &lt;/button&gt;
        &lt;div className=&quot;select-side&quot;&gt;
          &lt;div className=&quot;step-pages&quot;&gt;
            &lt;div className=&quot;sourceSelectButton&quot;&gt;
              &lt;div className=&quot;--active-number&quot;&gt;1&lt;/div&gt;
              &lt;span className=&quot;--active-step&quot;&gt;Select a Source&lt;/span&gt;
            &lt;/div&gt;
            &lt;div className=&quot;ConfigureButton&quot;&gt;
              &lt;div className=&quot;--inactive-number&quot;&gt;2&lt;/div&gt;
              &lt;span className=&quot;--inactive-step&quot;&gt;Configure&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;Reveal&gt;
            &lt;Searchbar /&gt;
          &lt;/Reveal&gt;
          &lt;Reveal&gt;
            &lt;ServicesComponent setSourceSelect={props.setSourceSelect} /&gt;
          &lt;/Reveal&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  ) : (
    &#39;&#39;
  );
};

const Configure = (props) =&gt; {
  return props.trigger ? (
    &lt;div className=&quot;popup&quot; onClick={() =&gt; props.setTrigger(false)}&gt;
      &lt;div className=&quot;popup-inner&quot; onClick={(e) =&gt; e.stopPropagation()}&gt;
        &lt;div className=&quot;side&quot;&gt;
          &lt;div className=&quot;logo&quot;&gt;
            &lt;MayaLogo name=&quot;HorizontalLogo&quot; size=&quot;112&quot; background=&quot;light&quot; /&gt;
          &lt;/div&gt;
          &lt;div className=&quot;petrol-image&quot;&gt;&lt;/div&gt;
          &lt;div className=&quot;side-text&quot;&gt;
            &lt;Reveal&gt;
              &lt;span className=&quot;grey-text&quot;&gt;Step 2&lt;/span&gt;
            &lt;/Reveal&gt;
            &lt;Reveal&gt;
              &lt;h2 className=&quot;sourcePick&quot;&gt;Configure&lt;/h2&gt;
            &lt;/Reveal&gt;
            &lt;Reveal&gt;
              &lt;p className=&quot;grey-text&quot;&gt;
                You have no connector selected. Please select a source connector
                from our wide range of options to proceed
              &lt;/p&gt;
            &lt;/Reveal&gt;
          &lt;/div&gt;
        &lt;/div&gt;
        &lt;button className=&quot;close-btn&quot; onClick={() =&gt; props.setTrigger(false)}&gt;
          &lt;svg
            xmlns=&quot;http://www.w3.org/2000/svg&quot;
            width=&quot;24&quot;
            height=&quot;24&quot;
            viewBox=&quot;0 0 24 24&quot;
            fill=&quot;none&quot;
          &gt;
        &lt;/button&gt;
        &lt;div className=&quot;select-side&quot;&gt;
          &lt;div className=&quot;step-pages&quot;&gt;
            &lt;div className=&quot;sourceSelectButton&quot;&gt;
              &lt;div className=&quot;--active-number&quot;&gt;1&lt;/div&gt;
              &lt;span className=&quot;--active-step&quot;&gt;Select a Source&lt;/span&gt;
            &lt;/div&gt;
            &lt;div className=&quot;ConfigureButton&quot;&gt;
              &lt;div className=&quot;--inactive-number&quot;&gt;2&lt;/div&gt;
              &lt;span className=&quot;--inactive-step&quot;&gt;Configure&lt;/span&gt;
            &lt;/div&gt;
          &lt;/div&gt;
          &lt;Reveal&gt;
            &lt;Searchbar /&gt;
          &lt;/Reveal&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  ) : (
    &#39;&#39;
  );
};

function Popup(props) {
  const [Flag, setFlag] = useState(true);
  console.log(Flag);
  if (Flag) {
    return (
      &lt;SelectSource
        trigger={props.trigger}
        setTrigger={props.setTrigger}
        SourceSelect={Flag}
        setSourceSelect={setFlag}
      /&gt;
    );
  }
  if (!Flag) {
    return (
      &lt;Configure
        trigger={props.trigger}
        setTrigger={props.setTrigger}
        SourceSelect={Flag}
        setSourceSelect={setFlag}
      /&gt;
    );
  }
}

export default Popup;

Here , Reveal is just an animation component I created. As we can see I have set the Flag's Initial state to be true, and the props are the trigger and setTrigger state functionality for opening and closing the popup.

I then pass the flag and SetFlag to these both components and the services components are the button icon and if we click the button we should be able to go to the next page(i.e Step 2 Configure page). But, when I console logged the value of Flag, I got it as undefined.
如何在React中对弹出窗口进行条件渲染。

The code for the services component is:

import React from &#39;react&#39;;
import &#39;./ServicesComponent.css&#39;;

import { companies } from &#39;../constants&#39;;
import Image from &#39;next/image&#39;;
const ServicesComponent = (props) =&gt; {
  return (
    &lt;div className=&quot;servicesList&quot;&gt;
      {companies.map((service, index) =&gt; {
        return (
          &lt;div className=&quot;element_service&quot;&gt;
            &lt;span className=&quot;service_title&quot; id={service.index}&gt;
              {service.title}
            &lt;/span&gt;

            &lt;div className=&quot;outer-box&quot;&gt;
              {service.company.map((value, index_v) =&gt; (
                &lt;button
                  onClick={props.setSourceSelect(props.SourceSelect, false)}
                &gt;
                  &lt;div className=&quot;box1&quot; key={index_v}&gt;
                    &lt;Image
                      src={value.icon}
                      alt=&quot;company_img&quot;
                      className=&quot;company_img&quot;
                    /&gt;
                    &lt;div className=&quot;box2&quot;&gt;
                      &lt;span className=&quot;company_name&quot;&gt;{value.name}&lt;/span&gt;
                      &lt;span className=&quot;desc&quot;&gt;{value.description}&lt;/span&gt;
                    &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/button&gt;
              ))}
            &lt;/div&gt;
          &lt;/div&gt;
        );
      })}
      &lt;div className=&quot;list&quot;&gt;&lt;/div&gt;
    &lt;/div&gt;
  );
};

export default ServicesComponent;

I dont know what is wrong with my code. If anybody can provide any insights it will be really helpful.

答案1

得分: 1

我看到你的代码中传递了两个参数给setFlaguseState函数。这会引发错误,因为它只期望一个参数。

你的代码:

<button
     onClick={props.setSourceSelect(props.SourceSelect, false)}
>

而这是正确的写法:

<button
     onClick={props.setSourceSelect(!props.SourceSelect)}
>

这样,如果状态为true,你就将状态设置为false,如果状态为false,则设置为true,实现内容的切换。

英文:

Looking at your code i've seen that you are passing two arguments to the setFlag useState function. This will cause an error because it is expecting just one argument.

this is your code:

&lt;button
     onClick={props.setSourceSelect(props.SourceSelect, false)}
&gt;

And this is the correct one:

&lt;button
     onClick={props.setSourceSelect(!props.SourceSelect)}
&gt;

This way you set the flag state to false if it is true and true if it is false, switching between your content.

huangapple
  • 本文由 发表于 2023年7月13日 15:08:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76676763.html
匿名

发表评论

匿名网友

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

确定