简体   繁体   English

尝试在其他类中重用组件并获取错误:“警告:setState(...):只能更新已安装或安装的组件。 ”

[英]Trying to re-use component in other class and getting error: “Warning: setState(…): Can only update a mounted or mounting > component. ”

I'm putting together a little POC, where one piece of it is executing a Search function. 我正在组建一个小POC,其中一个正在执行搜索功能。 The idea is that "Search" will be responsible for the following things: 我们的想法是“搜索”将负责以下事项:
- Displaying the search input form (eg, text, date and locations parameters) - 显示搜索输入表单(例如,文本,日期和位置参数)
- Hit the backend AWS Lambda search API - 点击后端AWS Lambda搜索API
- Return the result object back to the Search object - 将结果对象返回到Search对象
- Be able to be re-used on multiple pages - 能够在多个页面上重复使用

I have two different pages that I want to leverage the "search" functionality, but render the results in different ways. 我有两个不同的页面,我想利用“搜索”功能,但以不同的方式呈现结果。

The search component/form works stand-alone, but I can't figure out how to embed it on the other web pages. 搜索组件/表单独立运行,但我无法弄清楚如何将其嵌入其他网页。 Whenever I try to input anything into the "Search form" the console throws the following error: 每当我尝试在“搜索表单”中输入任何内容时,控制台都会抛出以下错误:

index.js:2178 Warning: setState(...): Can only update a mounted or mounting > component. index.js:2178警告:setState(...):只能更新已安装或已安装的组件。 This usually means you called setState() on an unmounted component. 这通常意味着您在已卸载的组件上调用了setState()。 > This is a no-op. >这是一个无操作。

Please check the code for the Search component. 请检查搜索组件的代码。

The "Search" code is below, along with the start of page to display results. 下面是“搜索”代码,以及显示结果的页面开头。 I'm a novice to front-end dev so I may be doing something stupid here...looking for input on what I'm doing wrong! 我是前端开发人员的新手,所以我可能在这里做一些愚蠢的事情......寻找关于我做错了什么的输入! Thanks for the help. 谢谢您的帮助。

import React, { Component } from "react";
import {FormGroup, FormControl, ControlLabel } from "react-bootstrap";
import LoaderButton from "../components/LoaderButton";
import "./Home.css";
import DatePicker from 'react-datepicker';
import moment from 'moment';
import PlacesAutocomplete from 'react-places-autocomplete';
import { searchAssets } from "../libs/awsLib";

import 'react-datepicker/dist/react-datepicker.css';

// CSS Modules, react-datepicker-cssmodules.css
import 'react-datepicker/dist/react-datepicker-cssmodules.css';


export default class Search extends Component {
    constructor(props) {
        super(props);

        this.state = {
            isLoading: false,
            freeText: "",
            startDate: null,
            endDate: null,
            radius: 5,
            address: '',
            searchResults: null
        };

        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleStartDateChange = this.handleStartDateChange.bind(this);
        this.handleEndDateChange = this.handleEndDateChange.bind(this);
        this.handleLocationChange = this.handleLocationChange.bind(this);
    }

    // Basic change handling
    handleChange = event => {
        this.setState({ [event.target.id]: event.target.value });
    }

    // Location change needed because event.id cannot be taken from places object
    handleLocationChange = address => {
        console.log("The address has been changed: " + address);
        this.setState({ address: address });
    }

    // I need two separate handlers for each piece of the date range
    handleStartDateChange = date => {
        this.setState({ startDate: date });
    }

    handleEndDateChange = date => {
        this.setState({ endDate: date });
    }

    // 
    handleSubmit = async event => {
        event.preventDefault();
        console.log(event);

        // Construct the query string for AWS search

        var queryStatement = {
            accountId: 'account1', // Dummy
            groupId: 'group1', // Dummy
            caseId: '999', // Dummy
            freeText: this.state.freeText,
            radius: this.state.radius
        };

        if (this.state.startDate != null) {
            queryStatement['startDate'] = this.state.startDate.format("MM/DD/YYYY");
        }

        if (this.state.endDate != null) {
            queryStatement['endDate'] = this.state.endDate.format("MM/DD/YYYY");
        }

        if (this.state.address !== '') {
            queryStatement['address'] = this.state.address
        }

        console.log(queryStatement);

        this.setState({ isLoading: true });

        // Submit to the search API and load the Payload as a JSON object
        try {

            var resultSet;
            resultSet = await searchAssets(queryStatement);
            if (resultSet['StatusCode'] !== 200) {
                console.log("Error in lambda function");
            }
            console.log(JSON.parse(resultSet['Payload']));

            this.setState({
                isLoading: false,
                searchResults: JSON.parse(resultSet['Payload'])
            });

        }
        catch (e) {
            console.log(e);
            this.setState({ isLoading: false });
        }
    }

    render() {

        const autoLocationProps = {
          value: this.state.address,
          onChange: this.handleLocationChange,
        }

        console.log(this.state);

        // Only fetch suggestions when the input text is longer than 3 characters.
        const shouldFetchSuggestions = ({ value }) => value.length > 3


        return (
            <div className="Search">
                <form onSubmit={this.handleSubmit}>
                    <FormGroup controlId="freeText" bsSize="large">
                        <ControlLabel>Enter text to search on</ControlLabel>
                        <FormControl
                            type="textarea"
                            onChange={this.handleChange}
                            value={this.state.freeText}
                            placeholder="Enter any values to search on"
                        />
                    </FormGroup>
                    <FormGroup controlId="address" bsSize="large">
                        <ControlLabel>Enter search location</ControlLabel>
                        <PlacesAutocomplete 
                            inputProps={autoLocationProps}
                            shouldFetchSuggestions={shouldFetchSuggestions}
                            placeholderText="Start typing an address"
                        />
                    </FormGroup>
                    <FormGroup controlId="radius" bsSize="large">
                        <ControlLabel>Enter search radius</ControlLabel>
                        <FormControl
                            onChange={this.handleChange}
                            type="text"
                            value={this.state.radius}
                        />
                    </FormGroup>
                    <FormGroup controlId="startDate" bsSize="large">
                        <ControlLabel>Enter start date</ControlLabel>
                        <DatePicker
                            onChange={this.handleStartDateChange}
                            selected={this.state.startDate}
                            placeholderText="Enter start date"
                            isClearable={true}
                            maxDate={this.state.endDate}
                        />
                    </FormGroup>
                        <FormGroup controlId="endDate" bsSize="large">
                        <ControlLabel>Enter end date</ControlLabel>
                        <DatePicker
                            onChange={this.handleEndDateChange}
                            selected={this.state.endDate}
                            placeholderText="Enter end date"
                            isClearable={true}
                            minDate={this.state.startDate}
                        />
                    </FormGroup>
                    <LoaderButton
                        block
                        bsSize="large"
                        type="submit"
                        isLoading={this.state.isLoading}
                        text="Search for files"
                        loadingText="Searching..."
                    />
                </form>
            </div>
        );
    }
}

View as a table: 以表格形式查看:

import React, { Component } from "react";
import {Table, thead, tbody, th, tr, td } from "react-bootstrap";
import Search from "./Search";
import "./Home.css";

export default class Viewfiles extends Component {
    constructor(props) {
        super(props);
    }  

    render() {   

        // Eventually we will render the results in the table...
        var resultsObject = new Search();

        return (
            <div className="Viewfiles">
                {resultsObject.render()}
                <hr/>
                <Table striped condensed responsive >
                    <thead>
                        <tr>
                        <th>Thumbnail</th>
                        <th>Checksum</th>
                        <th>Address</th>
                        <th>Checksum</th>
                        <th>Description</th>
                        </tr>
                    </thead>
                </Table>
            </div>
        );
    }
}

React components are meant to be rendered in JSX, like <Search /> . React组件应在JSX中呈现,如<Search /> If you instantiate them on your own, React won't know to mount them properly, with state and everything. 如果你自己实例化它们,React将无法通过状态和所有内容正确地安装它们。

暂无
暂无

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

相关问题 警告:setState(…):只能更新已安装或正在安装的组件。 这通常意味着 - Warning: setState(…): Can only update a mounted or mounting component. This usually means 只能更新已安装或正在安装的组件。 - Can only update a mounted or mounting component. 只能更新已安装或正在安装的组件。 找不到错误 - Can only update a mounted or mounting component. findless error 反应警告:setState(…)只能更新已安装或正在安装的组件 - React Warning: setState(…) Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件 - Warning react : setState(…): Can only update a mounted or mounting component 警告:setState(…):只能更新已安装或正在安装的组件,如何卸载? - Warning: setState(…): Can only update a mounted or mounting component, How to unmount? 警告:setState(...):只能更新已安装或安装的组件 - Warning: setState(…): Can only update a mounted or mounting component 反应警告setState(…):只能更新已安装或正在安装的组件 - React warning setState(…): Can only update a mounted or mounting component setState(...):只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState()。 这是一个无操作 - setState(…): Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component. This is a no-op 只能更新已安装或安装的组件。 这通常意味着您在已卸载的组件上调用了setState() - Can only update a mounted or mounting component. This usually means you called setState() on an unmounted component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM