如何在React组件中更改属性值以关闭我的弹出窗口?

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

How can I change prop value in react component to close my popup?

问题

我正在尝试创建一个弹出功能,但不使用Redux。

我的弹出组件接收一个名为activePopup的prop,其值为true或false。
当它为true时,我的弹出窗口会显示出来,但我无法将此prop更改为false以关闭弹出窗口,因为activeProp只能在父组件中更改。

我正在尝试修复我的弹出组件,以使它可以按我想要的方式工作。因为我的弹出窗口可以打开,但无法在组件内部关闭。

英文:

I'm trying to make a popup functionality but without Redux.

My popup component gets a prop called activePopup and width values true or false.
When it's true, my popup is showing up, but I can't change this prop to false for
closing this popup, because activeProp can be change only in the parent component.

const Popup = ({ isActive }) => {

  return (
    <div className={isActive ? "popup" : ""}>
      <div className="popupContent">
        <h2>
          Welcome to popup!
          <button title="close popup">X</button>
        </h2>

        <p>
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
          cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
          eos harum similique, architecto a minus explicabo atque error.
        </p>
      </div>
    </div>
  );
};

I trying to fix my popup component so that he/she can work like I want. Because my popup can open, but it can't close inside the component.

答案1

得分: 0

将(可能)在父组件中管理的状态通过 hooks 转移到组件的本地状态。

import React, { useState } from 'react';

const Popup = () => {
  const [isActive, setIsActive] = useState(false);

  return (
    <div className={isActive ? "popup" : ""}>
      <div className="popupContent">
        <h2>
          欢迎使用弹出窗口
          <button title="关闭弹出窗口" onClick={() => setIsActive(false)}>X</button>
        </h2>

        <p>
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
          cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
          eos harum similique, architecto a minus explicabo atque error.
        </p>
      </div>
    </div>
  );
};
英文:

Transfer the state that is (probably) managed in your parent component into the local state of the component via hooks.

import React, {useState} from &#39;react&#39;

const Popup = () =&gt; {
  const [isActive, setIsActive] = useState(false)

  return (
    &lt;div className={isActive ? &quot;popup&quot; : &quot;&quot;}&gt;
      &lt;div className=&quot;popupContent&quot;&gt;
        &lt;h2&gt;
          Welcome to popup!
          &lt;button title=&quot;close popup&quot; onClick={() =&gt; setIsActive(false)}&gt;X&lt;/button&gt;
        &lt;/h2&gt;

        &lt;p&gt;
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
          cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
          eos harum similique, architecto a minus explicabo atque error.
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

答案2

得分: 0

four-eyes的答案很好,除了你不能再从父组件控制弹出窗口。你只需将状态和状态设置器作为props传递,并将你的代码与four-eyes的代码合并。

父组件:

const ParentComponent = ({ isActive, setIsActive }) => {
    const [isActive, setIsActive] = useState(false);

    return (
        <div>
            <button onClick={() => setIsActive(true)}>打开弹出窗口</button>
            <Popup isActive={isActive} setIsActive={setIsActive} />
        </div>
    );
}

弹出窗口组件:

const Popup = ({ isActive, setIsActive }) => {

  return (
    <div className={isActive ? "popup" : ""}>
      <div className="popupContent">
        <h2>
          欢迎来到弹出窗口!
          <button title="关闭弹出窗口" onClick={() => setIsActive(false)}>X</button>
        </h2>

        <p>
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
          cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
          eos harum similique, architecto a minus explicabo atque error.
        </p>
      </div>
    </div>
  );
};

现在你可以从组件内外控制状态了。

英文:

four-eyes's answer is good, except you can't control the popup from the parent anymore. You can just pass the state and the state-setter as props and merge your code with four-eyes&#39; code.

Parent Component:

const ParentComponent = ({ isActive, setIsActive }) =&gt; {
    const [isActive, setIsActive] = useState(false)

    return (
        &lt;div&gt;
            &lt;button onClick={()=&gt; setIsActive(true)}&gt; Open Popup &lt;/button&gt;
            &lt;Popup isActive={isActive} setIsActive={setIsActive} /&gt;
        &lt;/div&gt;
    )

}

Popup component

const Popup = ({ isActive, setIsActive }) =&gt; {

  return (
    &lt;div className={isActive ? &quot;popup&quot; : &quot;&quot;}&gt;
      &lt;div className=&quot;popupContent&quot;&gt;
        &lt;h2&gt;
          Welcome to popup!
          &lt;button title=&quot;close popup&quot; onClick={() =&gt; setIsActive(false)}&gt;X&lt;/button&gt;
        &lt;/h2&gt;

        &lt;p&gt;
          Lorem ipsum dolor sit, amet consectetur adipisicing elit. Odio neque
          cupiditate officiis cum rem iste aliquam ut ipsum quidem non, aperiam
          eos harum similique, architecto a minus explicabo atque error.
        &lt;/p&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  );
};

So now you can control the state from in and out of the component

huangapple
  • 本文由 发表于 2023年6月12日 19:20:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456156.html
匿名

发表评论

匿名网友

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

确定