简体   繁体   English

对象作为尝试返回 swal 的 React 子项(找到:[object Promise])无效

[英]Objects are not valid as a React child (found: [object Promise]) trying to return swal

I am trying to conditional rendering when state is undefined, and i would like to show a sweetalert to show the user that he has not selected a client.我正在尝试在 state 未定义时进行条件渲染,并且我想向用户展示他尚未选择客户端的甜蜜提醒。 But i am receiving this error:但我收到此错误:

Objects are not valid as a React child (found: [object Promise]).对象作为 React 子级无效(找到:[object Promise])。 If you meant to render a collection of children, use an array instead.如果您打算渲染一组子项,请改用数组。

Snippet:片段:

const location = useLocation();

const ClientNotSelected = () => {
    return (
        <div>
            {
                Swal.fire({
                    title: 'Are you sure?',
                    text: "You won't be able to revert this!",
                    icon: 'warning',
                    showCancelButton: true,
                    confirmButtonColor: '#3085d6',
                    cancelButtonColor: '#d33',
                    confirmButtonText: 'Yes, delete it!'
                }).then((result) => {
                    if (result.isConfirmed) {
                        Swal.fire(
                            'Deleted!',
                            'Your file has been deleted.',
                            'success'
                        )
                    }
                })

            }

        </div>

    )
}


if (location.state === undefined) {
    return <ClientNotSelected />;
} 

There is no need to create and render a component to fire the alert.无需创建和渲染组件来触发警报。

You can move Swal.fire() to your if statement.您可以将Swal.fire()移动到您的 if 语句中。

Like this:像这样:

if (location.state === undefined) {
  Swal.fire({
    title: "Are you sure?",
    text: "You won't be able to revert this!",
    icon: "warning",
    showCancelButton: true,
    confirmButtonColor: "#3085d6",
    cancelButtonColor: "#d33",
    confirmButtonText: "Yes, delete it!",
  }).then((result) => {
    if (result.isConfirmed) {
      Swal.fire("Deleted!", "Your file has been deleted.", "success");
    }
  });
}

HOW I SOLVED IT:我是如何解决的:

I have created我创造了

const [clientSelected, setClientSelected] = useState(false);

and i checked it in the useEffect, with swal.fire inside here it doesnt give any problems/errors我在useEffect中检查了它,里面有swal.fire它没有给出任何问题/错误

useEffect(()=>{
    if (location.state === undefined){
        setClientSelected(false);
        Swal.fire({
            title: 'Are you sure?',
            text: "You won't be able to revert this!",
            icon: 'warning',
            confirmButtonColor: '#3085d6',
            confirmButtonText: 'Ok'
        }).then((result) => {
            if (result.isConfirmed) {
                history.goBack();
            }
        })
    }
    else{
        setClientSelected(true);
    }
})

below i have continued with the same condition but checking the new var clientSelected下面我继续使用相同的条件,但检查新的 var clientSelected

if (!clientSelected) {
    return <h1>Select a client</h1>;
} else {
    return <Component />;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 对象作为 React 子级无效(发现:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) 对象无效,无法作为React子对象(找到:[object Promise]) - Objects not valid as a React child (found: [object Promise]) React 错误“对象作为 React 子项无效(找到:[object Promise])” - React error "Objects are not valid as a React child (found: [object Promise])" 对象在反应渲染中作为 React 子级无效(找到:[object Promise]) - Objects are not valid as a React child (found: [object Promise]) in react render 尝试使用爬网数据,但错误显示为“对象作为 React 子项无效(找到:[object Promise])” - trying to use crawled data but error appears like 'Objects are not valid as a React child (found: [object Promise])' 错误:对象在 reactjs 中作为 React 子对象无效(找到:[object Promise]) - Error: Objects are not valid as a React child (found: [object Promise]) in reactjs Redux:对象作为 React 子项无效(找到:[object Promise]) - Redux: Objects are not valid as a React child (found: [object Promise]) 我收到一个错误:对象作为 React 子对象无效(找到:[object Promise]) - I got an Error: Objects are not valid as a React child (found: [object Promise]) 如何修复错误:对象作为 React 子对象无效(找到:[object Promise]) - How to fix Error: Objects are not valid as a React child (found: [object Promise]) 解决功能组件中的“对象作为 React 子级无效(找到:[object Promise])” - Resolving “Objects are not valid as a React child (found: [object Promise])” in Functional Components
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM