0

I am new to React JS still learning a lot. I created another js file (Loading.js) which is I want it to be a loader on my onClick event specifically on (New Client button) && (Manage Account button)

import React from 'react';
import Loading from './Loading';
import '../Styles.scss';
    Modal,
    Icon,
} from 'semantic-ui-react';

function InvestNow() {
  return (
    <div>
      <Button
        onClick={() => {
          window.location.href = '/';
          //* ADD Loading.js here
        }}
        color="yellow"
        fluid
        icon
        labelPosition="left">
        <Icon name="plus" />
        New Client
      </Button>
    </div>
  );
}

export default InvestNow

This is the Loader I created from react-loader-spinner

My Loading.js


import React, { Component } from 'react';
import Loader from 'react-loader-spinner';
import '../Styles.scss';
import 'react-loader-spinner/dist/loader/css/react-spinner-loader.css';

class Loading extends React.Component {
  render() {
    return (
      <Loader
        type="Bars"
        color="#FFD400"
        height={450}
        width={100}
        timeout={5000} //3 secs
        className="Loadingstyle"
      />
    );
  }
}

export default Loading;
3
  • So on click you want to display a loading animation? This can be done by creating a state in InvestNow component to track whether or not the loading should be shown const [loading, setLoading] = React.useState(false); you can then set loading to true in your click setLoading(true); To display your loader you can conditionally display it using { !!loading && <Loading /> } Commented Oct 20, 2020 at 2:39
  • yes right @JacobSmit Commented Oct 20, 2020 at 2:40
  • do you use router ? do you use redux mobx or any other state manage library ? Commented Oct 20, 2020 at 2:42

2 Answers 2

1

Actually what you want is to maintain a state to toggle loading indicator. For that import "useState" from react. And then use the below code as reference.

<Button
onClick={() => {
    setLoading(true);
    setTimeout(function(){ 
       setLoading(false);
       window.location.href='/';
    }, 5000);
}}
>
    {isLoading ? <Loading /> : null }
    <Icon name="plus" />
    New Client
</Button>

You also need to maintain state to handle toggling loading indicator. Inside "InvestNow" function write this code.

const [isLoading, setLoading] = useState(false);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for having your time to answer. I think my question is Misleading Sorry for that. This would be the sequence: After I click the button, my Loader will appear ( which is I set for 5sec) then will redirect to another js file. Thank you so much
0

If I understood correctly, you want to show your Loading.js component instead of the button after onClick is called. If so, you actually need to add an state management to your component. Something like

function InvestNow() {
  // Variable to control whether you should render loading or button
  const [isLoading, setIsLoading] = React.useState(false)

  return (
    <div>
      {
        // Will render Button when not loading, and Loading component otherwise
        !isLoading ? (
          <Button
            onClick={() => {
              // Sets loading to true
              setIsLoading(true)

              // Do other things
            }}
            /* ... */
          />
        ) : (
          <Loading
            /* ... */
          />
        )
      }
    </div>
  )
}

For React.useState reference: https://reactjs.org/docs/hooks-reference.html#usestate

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.