简体   繁体   English

我不断收到 Can't perform a React state update on an unmounted component

[英]I keep getting Can't perform a React state update on an unmounted component

I keep getting this error for two of my React components and i can't figure it out.我的两个 React 组件不断收到此错误,但我无法弄清楚。 I just started to learn to use hooks and I can't seem to fix this error message.我刚开始学习使用钩子,但似乎无法修复此错误消息。

I tried searching online and going through a few suggested methods but it doesn't seem to work for me.我尝试在线搜索并通过一些建议的方法,但它似乎对我不起作用。

I saw an article saying to add this code below but it has no effect at all.我在下面看到一篇文章说要添加此代码,但它根本没有效果。

let mounted = true

return function cleanup() {
            mounted = false
        }

Here's the two errors:这是两个错误:

  1. "index.js:1 Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in BoardAdmin (at App.js:125) in component (at PrivateRoute.js:9)" “index.js:1 警告:无法在未安装的组件上执行 React state 更新。这是一个无操作,但它表明您的应用程序中存在 memory 泄漏。要修复,请取消 useEffect 中的所有订阅和异步任务清理 function. 在 BoardAdmin(在 App.js:125)中的组件(在 PrivateRoute.js:9)”

  2. "Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in SFTPList (at CsvExtractor.js:206) in div (created by Col) in Col (at CsvExtractor.js:205) in div (created by Row) in Row (at CsvExtractor.js:183) in form (created by Form) in Form (at CsvExtractor.js:129) in CsvExtractor (at BoardAdmin.js:30) in header (at BoardAdmin.js:29) in div (at BoardAdmin.js:28) in BoardAdmin (at App.js:125) in component (at PrivateRoute.js:9)" "Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function. in SFTPList (at CsvExtractor.js:206) in div (由 Col 创建) in Col (at CsvExtractor.js:205) in div (由 Row 创建) in Row (at CsvExtractor.js:183) in form (由 Form 创建) in表单(在 CsvExtractor.js:129)在 CsvExtractor(在 BoardAdmin.js:30)在 header(在 BoardAdmin.js:29)在 div(在 BoardAdmin.js:28)在 BoardAdmin(在 App.js:125)中组件(在 PrivateRoute.js:9)”

csvextractor.js csvextractor.js

import React, { useState, useEffect, useRef } from 'react';
import Dropzone from 'react-dropzone';
import MultiSelect from 'react-multiple-select-dropdown-lite';
import 'react-multiple-select-dropdown-lite/dist/index.css';
import 'react-datepicker/dist/react-datepicker.css';
import DatePicker from 'react-datepicker';
import SFTPList from './SFTPDownload';
import { Form, Row, Col, Button } from 'react-bootstrap';
import FileService from '../services/file.service';

import { history } from '../helpers/history';

const CsvExtractor = () => {
  const [file, setFile] = useState(null); // state for storing actual file
  const [details, setDetails] = useState({
    title: '',
    description: '',
  });
  const [errorMsg, setErrorMsg] = useState('');
  const dropRef = useRef(); // React ref for managing the hover state of droppable area
  const [startDate, setStartDate] = useState(new Date());
  const [endDate, setEndDate] = useState(null);
  const [filters, setFilters] = useState([]);

  const filterOptions = [
    {
      value: 'translate',
      label: 'Translate to EN',
    },
    {
      value: 'emailentrant',
      label: 'Email Entrant',
    },
    {
      value: 'emailsortant',
      label: 'Email Sortant',
    },
    {
      value: 'appelentrant',
      label: 'Appel Entrant',
    },
    {
      value: 'appelsortant',
      label: 'Appel Sortrant',
    },
    {
      value: 'chat',
      label: 'Chat',
    },
  ];

  const handleSelect = (selections) => {
    setDetails({
      ...details,
      description: `${selections}`,
    });
    setFilters(selections.split(','));
  };

  const handleInputChange = (event) => {
    setDetails({
      ...details,
      [event.target.name]: event.target.value,
    });
  };

  const onDrop = (files) => {
    const [uploadedFile] = files;
    setFile(uploadedFile);

    const fileReader = new FileReader();
    fileReader.onload = () => {};
    fileReader.readAsDataURL(uploadedFile);
    dropRef.current.style.border = '2px dashed #e9ebeb';
  };

  const onChange = (dates) => {
    const [start, end] = dates;
    const tdate = end === null ? '' : end.toString().slice(4, 15);
    setDetails({
      ...details,
      title: `${start.toString().slice(4, 15)} - ${tdate}`,
    });
    setStartDate(start);
    setEndDate(end);
  };

  const updateBorder = (dragState) => {
    if (dragState === 'over') {
      dropRef.current.style.border = '2px solid #000';
    } else if (dragState === 'leave') {
      dropRef.current.style.border = '2px dashed #e9ebeb';
    }
  };

  const handleOnSubmit = async (event) => {
    event.preventDefault();

    try {
      const { title, description } = details;

      if (title.trim() !== '' && description.trim() !== '') {
        if (file) {
          const formData = new FormData();
          formData.append('startDate', startDate);
          formData.append('endDate', endDate);
          formData.append('filters', filters);
          formData.append('file', file);
          formData.append('title', title);
          formData.append('description', description);

          setErrorMsg('');

          await FileService.uploadFile(formData);
          history.push('/list');
        } else {
          setErrorMsg('Please select a file to add.');
        }
      } else {
        setErrorMsg('Please enter all the field values.');
      }
    } catch (error) {
      error.response && setErrorMsg(error.response.data);
    }
  };

  return (
    <React.Fragment>
      <Form className="search-form">
        {errorMsg && <p className="errorMsg">{errorMsg}</p>}
        <Row>
          <Col>
            <Form.Group controlId="title">
              <Form.Control
                type="text"
                name="title"
                value={details.title || ''}
                placeholder="Enter title"
                onChange={handleInputChange}
              />
            </Form.Group>
          </Col>
        </Row>
        <Row>
          <Col>
            <Form.Group controlId="description">
              <Form.Control
                type="text"
                name="description"
                value={details.description || ''}
                placeholder="Enter description"
                onChange={handleInputChange}
              />
            </Form.Group>
          </Col>
        </Row>
        <Row>
          <Col>
            <div>
              <h5>Select date range</h5>
            </div>
            <DatePicker
              selected={startDate}
              onChange={onChange}
              startDate={startDate}
              endDate={endDate}
              selectsRange
              inline
            />
          </Col>

          <Col>
            <h5>Select filters (at least one)</h5>

            <MultiSelect
              style={{ backgroundColor: 'white', marginTop: '10px' }}
              onChange={handleSelect}
              options={filterOptions}
            />
          </Col>
        </Row>

        <Row>
          <Col xs={12}>
            {/* <div className="upload-section"> */}
            <Dropzone
              onDrop={onDrop}
              onDragEnter={() => updateBorder('over')}
              onDragLeave={() => updateBorder('leave')}
            >
              {({ getRootProps, getInputProps }) => (
                <div {...getRootProps({ className: 'drop-zone' })} ref={dropRef}>
                  <input {...getInputProps()} />
                  <p>Drag and drop a file OR click here to select a file</p>
                  {file && (
                    <div>
                      <strong>Selected file:</strong> {file.name}
                    </div>
                  )}
                </div>
              )}
            </Dropzone>
            {/* </div> */}
          </Col>
          <Col>
            <SFTPList />
          </Col>
        </Row>
        <Button variant="primary" type="submit" onClick={handleOnSubmit}>
          Submit
        </Button>
      </Form>
    </React.Fragment>
  );
};

export default CsvExtractor;

adminboard.js is below adminboard.js 如下

import React, { useState, useEffect, useRef } from 'react';
import 'react-multiple-select-dropdown-lite/dist/index.css';
import 'react-datepicker/dist/react-datepicker.css';

import UserService from '../services/user.service';
import CsvExtractor from './CsvExtractor';

const BoardAdmin = () => {
  const [content, setContent] = useState('');

  useEffect(() => {
    UserService.getAdminBoard().then(
      (response) => {
        setContent(response.data);
      },
      (error) => {
        const _content =
          (error.response && error.response.data && error.response.data.message) ||
          error.message ||
          error.toString();

        setContent(_content);
      },
    );
  }, []);

  return (
    <div className="container">
      <header className="jumbotron">
        <CsvExtractor />
      </header>
    </div>
  );
};

export default BoardAdmin;

If fixed it with the code below如果用下面的代码修复它

  const [filesList, setFilesList] = useState([]);
  const [errorMsg, setErrorMsg] = useState('');

  useEffect(() => {
    const source = axios.CancelToken.source();
    const getFilesList = async () => {
      try {
        const { data } = await axios.get('api/file/getallfiles/', {
          headers: authHeader(),
          cancelToken: source.token,
        });
        setErrorMsg('');
        setFilesList(data);
      } catch (error) {
        if (axios.isCancel(error)) {
        } else {
          error.response && setErrorMsg(error.response.data);
        }
      }
    };
    getFilesList();
    return () => {
      source.cancel();
    };
  }, []);

暂无
暂无

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

相关问题 为什么我收到以下 React Native 警告:Can&#39;t perform a React state update on an unmounted component - Why am I getting the following React Native warning: Can't perform a React state update on an unmounted component 构建一个反应应用程序但收到一条我找不到的错误消息:警告:无法在未安装的组件上执行 React state 更新 - Building a react app but getting an error message that I can't locate: Warning: Can't perform a React state update on an unmounted component React - 无法在未安装的组件上执行 React state 更新 - React - Can't perform a React state update on an unmounted component React Can't perform a React state update on an unmounted component error - React Can't perform a React state update on an unmounted component error React Native:无法对未安装的组件执行 React state 更新 - React Native: Can't perform a React state update on an unmounted component React 导致:无法对未安装的组件执行 React state 更新 - React causing: Can't perform a React state update on an unmounted component React:无法在未安装的组件上执行 React state 更新 - React: Can't perform a React state update on an unmounted component 导航问题 - 无法对卸载的组件执行 React 状态更新 - Navigation issue - can't perform a React state update on unmounted component 如何解决“无法对未安装的组件执行反应状态更新” - How to solve "can't perform a react state update on an unmounted component" 无法在未安装的组件主题提供程序上执行 React state 更新 - Can't perform a React state update on an unmounted component theme provider
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM