简体   繁体   English

在反应中有条件地呈现下载按钮

[英]Conditionally render download button in react

I have a download button from which the user can download the assets.我有一个下载按钮,用户可以从中下载资产。 but I want only authenticated users can download.但我希望只有经过身份验证的用户才能下载。 If the user is not authenticated and click on the download button I want to show them a modal with a login button on it.如果用户未通过身份验证并单击下载按钮,我想向他们展示一个带有登录按钮的模式。 I have a modal component.我有一个模态组件。 I am using React .我正在使用反应 Please help me.请帮我。

Getting currently authenticated user from the firebase.从 firebase 获取当前经过身份验证的用户。

const {currentUser} = useAuth();

This is my code for the download button.这是我的下载按钮代码。

  <a className="bg-secondary text-white font-bold py-3 
     px-5 rounded text-2xl focus:outline-none mt-3 block w-48" href="YOUR_URL">
  <i className="animate-bounce fas fa-arrow-down mr-2"></i>
    Download
</a>

Modal Code:模态代码:

import React from "react";
import { useState } from "react";

export default function Modal() {
  const [showModal, setShowModal] = useState(true);
  console.log('modal');
  return (
    <>
      {showModal ? (
        <>
          <div
            className="justify-center items-center flex overflow-x-hidden overflow-y-auto fixed inset-0 z-50 outline-none focus:outline-none"
            onClick={() => setShowModal(false)}
          >
            <div className="relative w-auto my-6 mx-auto max-w-3xl">
              {/*content*/}
              <div className="border-0 rounded-lg shadow-lg relative flex flex-col w-full bg-white outline-none focus:outline-none">
                {/*header*/}
                <div className="flex items-start justify-between p-5 border-b border-solid border-gray-300 rounded-t">
                  <h3 className="text-3xl font-semibold">
                    Modal Title
                  </h3>
                  <button
                    className="p-1 ml-auto bg-transparent border-0 text-black opacity-5 float-right text-3xl leading-none font-semibold outline-none focus:outline-none"
                    onClick={() => setShowModal(false)}
                  >
                    <span className="bg-transparent text-black opacity-5 h-6 w-6 text-2xl block outline-none focus:outline-none">
                      ×
                    </span>
                  </button>
                </div>
                {/*body*/}
                <div className="relative p-6 flex-auto">
                  <p className="my-4 text-gray-600 text-lg leading-relaxed">
                    I always felt like I could do anything. That’s the main
                    thing people are controlled by! Thoughts- their perception
                    of themselves! They're slowed down by their perception of
                    themselves. If you're taught you can’t do anything, you
                    won’t do anything. I was taught I could do everything.
                  </p>
                </div>
                {/*footer*/}
                <div className="flex items-center justify-end p-6 border-t border-solid border-gray-300 rounded-b">
                  <button
                    className="text-red-500 background-transparent font-bold uppercase px-6 py-2 text-sm outline-none focus:outline-none mr-1 mb-1"
                    type="button"
                    style={{ transition: "all .15s ease" }}
                    onClick={() => setShowModal(false)}
                  >
                    Close
                  </button>
                  <button
                    className="bg-green-500 text-white active:bg-green-600 font-bold uppercase text-sm px-6 py-3 rounded shadow hover:shadow-lg outline-none focus:outline-none mr-1 mb-1"
                    type="button"
                    style={{ transition: "all .15s ease" }}
                    onClick={() => setShowModal(false)}
                  >
                    Save Changes
                  </button>
                </div>
              </div>
            </div>
          </div>
          <div className="opacity-25 fixed inset-0 z-40 bg-black"></div>
        </>
      ) : null}
    </>
  );
}

You could use button instead of a and use onClick to check if currentUser exists when you click button.您可以使用button而不是a并使用 onClick 来检查单击按钮时 currentUser 是否存在。

This is final code:这是最终代码:

function x {

const {currentUser} = useAuth();

const downloadAssets = () => {
   if(currentUser){
      window.open("url assets here", '_blank', 'noopener,noreferrer')
   }else{
     // Open modal here
     alert("User not authenticated"); 
   }
}
return {
<>
  ...
  <button onClick={downloadAssets}><i className="animate-bounce fas fa-arrow-down mr-2"></I>Download</button>
  ...
</>
}

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

相关问题 如何通过 map 函数有条件地使用 React 和 Firebase 渲染元素? - How can I conditionally render elements with React and Firebase via map function? 错误:有条件地调用 React Hook“useDocumentOnce”。 React Hooks 必须在每个组件渲染中以完全相同的顺序调用 - Error: React Hook "useDocumentOnce" is called conditionally. React Hooks must be called in the exact same order in every component render React.js - 有条件地调用 useDocument 挂钩 - React.js - call useDocument hook conditionally Function 返回不会在 React 中呈现链接 - Function return will not render Links in React 如何暂停渲染直到在反应中获取数据 - How to pause render until the data is fetched in react 如何即时计算 + 下载文件 - React / Django? - How to compute on the fly + download a file - React / Django? 无法在主机上的 React 应用程序中下载 pdf - Unable to download pdf in react application on host 如何在 React useEffect 和 firebase 中正确渲染组件 - How To render properly a component in react useEffect and firebase 使用 express 和 React 应用程序从 aws 下载文件夹 - download folder from aws with express and React app 在反应中渲染之前检查条件是否满足 - Check if a condition is satisfied before render in react
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM