小编dan*_*ong的帖子

Antd 通知出现两次?

我正在使用 antd 通知组件,如下所示

        import { notification } from "antd";

        const Notification = ({ msg, type, clearMsg }) => {
          return (
            <div>
              {notification[type]({
                description: msg,
              })}
              {clearMsg()}
            </div>
          );
        };

        export default Notification;
Run Code Online (Sandbox Code Playgroud)

我只是在需要弹出通知的任何地方使用它。例如API响应失败后:

          const onSubmit = async (e) => {
            try {
              const res = await login(data);
              if (res.message) {
                setError({ state: true, msg: res.message });
              }
            } catch (err) {
              console.log(err);
            }
          };
Run Code Online (Sandbox Code Playgroud)

然后根据错误状态,我在返回正文中显示通知,如下所示:

          { error.state ?
            <Notification 
               type="error" 
               msg="some msg" 
               clearMsg={() => setError({state: false, msg: ""})} : …
Run Code Online (Sandbox Code Playgroud)

setstate reactjs antd

7
推荐指数
2
解决办法
5342
查看次数

为什么 for...of 等待 Promise 解析而 .forEach() 不等待?

我很难理解与处理 Promise 的情况有何for...of不同。.forEach()

使用这个片段:

const allSettled = async arr => {
    const data = []

    for (const promise of arr) {
        try {
            const result = await promise;
            data.push(result);
        } catch (e) {
            data.push(e)
        }
    }

    return data
}
Run Code Online (Sandbox Code Playgroud)

我检查数组中的每个承诺,等待它解决并将结果推送到data. 它按顺序执行。

如果我有这个片段:

const badAllSettled = arr => {
    const data = []

    arr.forEach(async promise => {
        try {
            const result = await promise;
            data.push(result);
        } catch (e) {
            data.push(e)
        }
    })

    return data
}
Run Code Online (Sandbox Code Playgroud)

我得到空数组(因为 …

javascript loops

5
推荐指数
1
解决办法
792
查看次数

标签 统计

antd ×1

javascript ×1

loops ×1

reactjs ×1

setstate ×1