简体   繁体   English

当我在反应中单击提交按钮时,如何显示加载指示器 5 秒?

[英]How to show loading indicator for 5 seconds when I click the submit button in react?

I am working on a React project.我正在做一个 React 项目。 In that project I am trying to save user details on local在那个项目中,我试图将用户详细信息保存在本地

Storage.贮存。 In this scenario after clicking submit button, I have to show the loader for showing在这种情况下单击提交按钮后,我必须显示加载程序以显示

Loader I installed react-loader-spinner so someone please help me how to show loader for five加载器我安装了 react-loader-spinner 所以有人请帮我如何显示加载器 5

Seconds after clicking the submit button.单击提交按钮后的几秒钟。

This is Users.js这是Users.js

import React, { Component } from 'react';
import './Users.css';
import { Container, Row, Col, Button } from 'reactstrap';
import UsersModal from '../../Components/UsersModal/UsersModal';
import Getusers from './Getusers/Getusers';

class Users extends Component {
    constructor(props) {
        super(props)

        this.state = {
            isModal: false,
        }
    }

    toggleUserModal = () => {
        this.setState({
            isModal: true
        })
    }

    handleCloseModal = () => {
        this.setState({
            isModal: false
        })
    }



    render() {
        return (
            <Container>
                <Row>
                    <Col>
                        <div className='mt-3'>
                            <Button onClick={this.toggleUserModal} outline color="secondary">Create User</Button>
                            <UsersModal openModal={this.state.isModal} closeModal={this.handleCloseModal}></UsersModal>
                        </div>
                    </Col>
                </Row>
            </Container>
        )
    }
}

export default Users 

This is UsersModals.js这是 UsersModals.js

import React, { Component } from 'react';
import './UsersModal.css';
import {
    Button,
    Col,
    Modal,
    ModalBody,
    ModalFooter,
    ModalHeader,
    Row,
    FormGroup,
    Label,
    Input,
} from 'reactstrap';

class UsersModal extends Component {
    constructor(props) {
        super(props)
        this.state= {
            users: null
        }
}

 handleChange = ({target}) => {
        const { name, value } = target;
        const newData = Object.assign({}, this.state.users, {[name]:value});
        this.setState({
            users: newData
        })
    }

handleSubmit = (e) => {
        e.preventDefault()
        localStorage.setItem('data', JSON.stringify(this.state.users))
        this.props.closeModal()
    }


render() {
        return (
            <Row>
                <Col md="6" sm="6" xs="6">

                    <Modal isOpen={this.props.openModal}
                    >
                        <ModalHeader >Create User</ModalHeader>
                        <ModalBody>
                            <FormGroup>
                                <Label for="exampleName">Name</Label>
                                <Input
                                    type='text'
                                    name='name'
                                    placeholder='Enter Your name'
                                    onChange={this.handleChange}
                                />

                            </FormGroup>
                            <FormGroup>
                                <Label for="exampleEmail">Email</Label>
                                <Input
                                    type='email'
                                    name='email'
                                    placeholder="Enter Your email"
                                    onChange={this.handleChange}
                                />
                            </FormGroup>
                        </ModalBody>
                        <ModalFooter>
                            <Button onClick={this.props.closeModal} color="secondary">
                                Cancel
                </Button>
                            <Button onClick={this.handleSubmit} type="submit" color="primary">
                                Submit
                </Button>
                        </ModalFooter>
                    </Modal>
                </Col>
            </Row>
        )
    }
}

export default UsersModal

I assume you'd like to show the loader inside the modal, above the form or near it..我假设您想在模态框内、表单上方或附近显示加载器..

Put the Loader component from the react-loader-spinner library where you'd like it to show, add a showLoader flag to your modal component's state and render the component in condition of the flag.react-loader-spinner库中的Loader组件放在您希望它显示的位置,将showLoader标志添加到您的模态组件的 state 并在该标志的条件下渲染组件。 Do not use the library's timeout prop.不要使用库的 timeout 道具。

{this.state.showLoader && (
  <Loader props />
)}

On your submit handler, set the flag to true to toggle the loader, and use a setTimeout to close it.在您的提交处理程序上,将标志设置为true以切换加载程序,并使用setTimeout关闭它。

handleSubmit = e => {
  e.preventDefault();
  localStorage.setItem("data", JSON.stringify(this.state.users));
  this.setState({
    showLoader: true
  });
  this.closeLoaderIn5Seconds();
};

closeLoaderIn5Seconds = () => 
  setTimeout(() => {
    this.props.closeModal();
    this.setState({
      showLoader: false
    });
  }, 5000);
};

Depending on where you'd like to show the loader you may have to move the code..根据您希望显示加载程序的位置,您可能需要移动代码..

I'd put the isModal flag to the child component to remove the little bug where the modal is closed after the loader..我将isModal标志放在子组件中,以删除在加载程序之后模式关闭的小错误。

Anyway here's a codesandbox .无论如何,这里有一个代码框

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM