【问题标题】:Reactjs update state after call component in functional componentReactjs在功能组件中调用组件后更新状态
【发布时间】:2022-01-10 23:28:52
【问题描述】:

我创建了一个名为Alertify.js的组件

import React, {useEffect, useState} from "react";
import { Alert } from 'reactstrap';

function Alertify(props){

    const [show, setShow] = useState(props.show);

        useEffect(
            () => {
                let timer = setTimeout(() => setShow(false), 3000);
                return () => {
                    clearTimeout(timer);
                };
            },
            []
        );

    return (
        <Alert color={props.color} className={show ? 'float active' : 'float'}>{props.text}</Alert>
    )
}

export default Alertify;

并用于index.js

import Alertify from "../Component/Alertify";
const [showAlert, setShowAlert] = useState(false);
return...
<Alertify text={'hello world'} color={'danger'} show={showAlert}/>

它会在条件为真后显示此警报:

if(condition){
setShowAlert(true)
}

但是出了点问题,并且没有显示警报,而且我是 reactjs 的新手,知道如何解决这个问题吗? 我想要的只是在条件为真后显示警报,然后在 3 秒后隐藏。

它还会显示我是否删除了useEffect,但在条件为真之前,并且也没有隐藏。

【问题讨论】:

  • 请问这个条件在哪里以及如何应用?编辑:另外,我不确定您是否使用了 useEffect,因为(根据您提供的代码)show 在渲染组件时似乎是错误的。
  • @thebigfundamentals 很容易。在此条件下单击 index.js 中的按钮后,我使用了setShowAlert(true)

标签: javascript reactjs ecmascript-6 jsx


【解决方案1】:

试试下面的

const [show, setShow] = useState(false);

useEffect(() => {
  if (props.show) {
   setShow(true)
  }
},[props.show])

您可以让现有的useEffect 在 3 秒后按原样清除。

编辑:

这是一种修改后的方法,您的 Alertify 组件看起来像这样

import React, { useEffect, useState } from "react";
import { Alert } from "reactstrap";

function Alertify(props: {
  show: any;
  color: string;
  text: boolean | React.ReactChild | React.ReactFragment | React.ReactPortal;
  setShowAlert: (value: boolean) => void;
}) {
  const [show, setShow] = useState(false);

  useEffect(() => {
    let timer = setTimeout(() => {
      return setShow(false);
    }, 3000);
    return () => {
      clearTimeout(timer);
    };
  });

  useEffect(() => {
    if (props.show) {
      setShow(true);
      props.setShowAlert(false);
    }
  }, [props.show, props.setShowAlert]);

  if (show) {
    return (
      <Alert color={props.color} className={show ? "float active" : "float"}>
        {props.text}
      </Alert>
    );
  }

  return null;
}

export default Alertify;

你的调用组件看起来像这样

import "./styles.css";
import Alertify from "./Alertify";
import { useState } from "react";

export default function App() {
  const [showAlert, setShowAlert] = useState(false);
  return (
    <>
      <div className="App">
        <Alertify
          text={"hello world"}
          color={"danger"}
          show={showAlert}
          setShowAlert={setShowAlert}
        />
      </div>
      <button onClick={() => setShowAlert(true)}>show alert</button>
    </>
  );
}

这是代码框链接https://codesandbox.io/s/alertify-stackoverflow-x096t

【讨论】:

  • 其实我没看懂你的回答,能不能多解释一下,在这里插入完整的代码?
  • @tourtravel 请看我上面的编辑。我还创建了一个代码框。
  • 谢谢它工作正常,只是它没有通过在类之间切换来显示 css 动画,我的意思是它显示柔和(淡入淡出)但在 3scs 后消失了,但这对我来说已经足够了,我接受了。感谢您的帮助。
【解决方案2】:

试试这个代码。您可以在 index.js 中控制渲染 Alertify 组件。如果 showAlert 值为 true,React 会渲染 Alertify 组件。当 setTimeout 执行时,showAlert 的值为 false,这意味着 React 卸载 Alertify 组件。这就像您需要的显示和隐藏效果。

// index.js
import Alertify from "../Component/Alertify";
const [showAlert, setShowAlert] = useState(false);

if (condition( {
  setShowAlert(true); // This makes Alertify component mount immediatly.
  setTimeout(setShowAlert(false),3000); // This makes Alertify component unmount in 3,000ms.
}

return...
{showAlert && <Alertify text={'hello world'} color={'danger'}/>}

因此,你不需要使用 useEffect 来隐藏这个组件。

// Alertify.js
import React, {useEffect, useState} from "react";
import { Alert } from 'reactstrap';

function Alertify(props){

    return (
        <Alert color={props.color} className={'float active'}>{props.text
        </Alert>
    )
};

export default Alertify;

【讨论】:

  • 但是我想做一个组件,不想在我使用的每个页面中隐藏 3000 毫秒后的警报,它应该隐藏在自己的组件中,你只是习惯将它隐藏在 index.js 但是如果我想在另一个页面中使用这个comp,比如x.js,我应该再次隐藏它,实际上它应该在alertify.js中
猜你喜欢
  • 2021-07-21
  • 2020-12-04
  • 2021-03-19
  • 2021-08-17
  • 2020-07-28
  • 2017-11-05
  • 2023-01-17
  • 2020-08-12
  • 1970-01-01
相关资源
最近更新 更多