英文:
How to perform conditional rendering on a Popup in react
问题
我正在使用React和Tailwind开发网站。我想在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的值时,它显示为未定义。
服务组件的代码如下:
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:
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 '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 className="popup-inner" onClick={(e) => e.stopPropagation()}>
<div className="side">
<div className="logo">
<MayaLogo name="HorizontalLogo" size="112" background="light" />
</div>
<div className="petrol-image"></div>
<div className="side-text">
<Reveal>
<span className="grey-text">Step 1</span>
</Reveal>
<Reveal>
<h2 className="sourcePick">Pick a Source</h2>
</Reveal>
<Reveal>
<p className="grey-text">
You have no connector selected. Please select a source connector
from our wide range of options to proceed
</p>
</Reveal>
</div>
</div>
<button className="close-btn" onClick={() => props.setTrigger(false)}>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
>
</button>
<div className="select-side">
<div className="step-pages">
<div className="sourceSelectButton">
<div className="--active-number">1</div>
<span className="--active-step">Select a Source</span>
</div>
<div className="ConfigureButton">
<div className="--inactive-number">2</div>
<span className="--inactive-step">Configure</span>
</div>
</div>
<Reveal>
<Searchbar />
</Reveal>
<Reveal>
<ServicesComponent setSourceSelect={props.setSourceSelect} />
</Reveal>
</div>
</div>
</div>
) : (
''
);
};
const Configure = (props) => {
return props.trigger ? (
<div className="popup" onClick={() => props.setTrigger(false)}>
<div className="popup-inner" onClick={(e) => e.stopPropagation()}>
<div className="side">
<div className="logo">
<MayaLogo name="HorizontalLogo" size="112" background="light" />
</div>
<div className="petrol-image"></div>
<div className="side-text">
<Reveal>
<span className="grey-text">Step 2</span>
</Reveal>
<Reveal>
<h2 className="sourcePick">Configure</h2>
</Reveal>
<Reveal>
<p className="grey-text">
You have no connector selected. Please select a source connector
from our wide range of options to proceed
</p>
</Reveal>
</div>
</div>
<button className="close-btn" onClick={() => props.setTrigger(false)}>
<svg
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
>
</button>
<div className="select-side">
<div className="step-pages">
<div className="sourceSelectButton">
<div className="--active-number">1</div>
<span className="--active-step">Select a Source</span>
</div>
<div className="ConfigureButton">
<div className="--inactive-number">2</div>
<span className="--inactive-step">Configure</span>
</div>
</div>
<Reveal>
<Searchbar />
</Reveal>
</div>
</div>
</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;
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.
The code for the services component is:
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">
<span className="service_title" id={service.index}>
{service.title}
</span>
<div className="outer-box">
{service.company.map((value, index_v) => (
<button
onClick={props.setSourceSelect(props.SourceSelect, false)}
>
<div className="box1" key={index_v}>
<Image
src={value.icon}
alt="company_img"
className="company_img"
/>
<div className="box2">
<span className="company_name">{value.name}</span>
<span className="desc">{value.description}</span>
</div>
</div>
</button>
))}
</div>
</div>
);
})}
<div className="list"></div>
</div>
);
};
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
我看到你的代码中传递了两个参数给setFlag
的useState
函数。这会引发错误,因为它只期望一个参数。
你的代码:
<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:
<button
onClick={props.setSourceSelect(props.SourceSelect, false)}
>
And this is the correct one:
<button
onClick={props.setSourceSelect(!props.SourceSelect)}
>
This way you set the flag state to false if it is true and true if it is false, switching between your content.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论