ReactJs:将数据获取代码或副作用移至 componentDidUpdate

标签 reactjs

我收到以下警告,但我不知道它意味着哪个数据更改,你知道吗?

Warning: componentWillReceiveProps has been renamed, and is not recommended for use. See react-unsafe-component-lifecycles for details.

* Move data fetching code or side effects to componentDidUpdate.
* If you're updating state whenever props change, refactor your code to use memoization techniques or move it to static getDerivedStateFromProps. Learn more at: 
* Rename componentWillReceiveProps to UNSAFE_componentWillReceiveProps to suppress this warning in non-strict mode. In React 17.x, only the UNSAFE_ name will work. To rename all deprecated lifecycles to their new names, you can run `npx react-codemod rename-unsafe-lifecycles` in your project source folder.

Please update the following components: t

代码:

import React, { Component } from "react";
import AppBar from "@material-ui/core/AppBar";
import Toolbar from "@material-ui/core/Toolbar";
import Typography from "@material-ui/core/Typography";
import Button from "@material-ui/core/Button";
import FacebookLogin from "react-facebook-login";
import Menu from "@material-ui/core/Menu";
import MenuItem from "@material-ui/core/MenuItem";

// import this
import { withStyles } from "@material-ui/core/styles";

// make this
const styles = theme => ({
  root: {
    flexGrow: 1
  },
  menuButton: {
    marginRight: theme.spacing(2)
  },
  title: {
    flexGrow: 1
  }
});

class App extends Component {
  state = {
    accessToken: "",
    isLoggedIn: false,
    userID: "",
    name: "",
    email: "",
    picture: ""
  };

  responseFacebook = response => {
    this.setState({
      accessToken: response.accessToken,
      isLoggedIn: true,
      userID: response.userID,
      name: response.name,
      email: response.email,
      picture: response.picture.data.url
    });
  };

  handleClick = event => this.setState({ anchorEl: event.currentTarget });
  handleClose = () => {
    this.setState({ anchorEl: undefined });
  };
  handleCloseAndLogOut = () => {
    this.setState({ anchorEl: undefined });
    this.setState({ isLoggedIn: undefined });
    this.setState({ userID: undefined });
    this.setState({ name: undefined });
    this.setState({ email: undefined });
    this.setState({ picture: undefined });
  };

  componentDidMount() {
    document.title = "Tiket.hu";
  }

  componentDidUpdate() {
    // What put here?
  }

  render() {
    let fbContent;
    let listContent;
    //const { anchorEl } = this.state;
    if (this.state.isLoggedIn) {
      fbContent = (
        <div>
          <Button
            aria-controls="simple-menu"
            aria-haspopup="true"
            onClick={this.handleClick}
          >
            {this.state.name}
          </Button>
          <Menu
            id="simple-menu"
            anchorEl={this.status.anchorEl}
            keepMounted
            open={Boolean(this.status.anchorEl)}
            onClose={this.handleClose}
          >
            <MenuItem onClick={this.handleCloseAndLogOut}>Log out</MenuItem>
            <MenuItem onClick={this.handleClose}>
              Switch mode to Release
            </MenuItem>
            <MenuItem onClick={this.handleClose}>My tickets</MenuItem>
          </Menu>
        </div>
      );
    } else {
      let fbAppId;
      if (
        window.location.hostname === "localhost" ||
        window.location.hostname === "127.0.0.1"
      )
        fbAppId = "402670860613108";
      else fbAppId = "2526636684068727";
      fbContent = (
        <FacebookLogin
          appId={fbAppId}
          autoLoad={true}
          fields="name,email,picture"
          onClick={this.componentClicked}
          callback={this.responseFacebook}
        />
      );
    }
    return (
      <div className="App">
        <AppBar position="static">
          <Toolbar>
            <Typography variant="h6" className={this.props.classes.title}>
              Tiket.hu
            </Typography>
            <Button color="inherit">Search</Button>
            <Button color="inherit">Basket</Button>
            {fbContent}
          </Toolbar>
        </AppBar>
        {listContent}
      </div>
    );
  }
}

export default withStyles(styles)(App);

最佳答案

您显示的组件没有 componentWillReceiveProps,所以这不是问题所在。错误消息包括:

Please update the following components: t

所以我们正在寻找一个名为t 的组件。如果您有一个具有该名称的,那就是您需要修复的那个。但我的猜测是,这不是您的组件之一,而是您导入的库之一间接使用的组件。如果是这种情况,只有库创建者才能解决这个问题,然后您可以导入他们代码的新版本。

此警告不会阻止您的代码在当前版本的 React 上运行,但会阻止您升级到 React 17 版本(目前尚不存在)

关于ReactJs:将数据获取代码或副作用移至 componentDidUpdate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59160779/

相关文章:

javascript - Material Ui popover 不在正确的位置

reactjs - let import React from 'react/addons' 和 import { PropTypes } from 'react/addons' 之间的区别

javascript - 如何在删除数据库时 react 重新渲染 ListView

reactjs - 使用 ReactSelector for TestCafe 时不断收到 `ReExecutablePromise { _then: [], _fn: [Function], _taskPromise: null }`

reactjs - 错误 : You may need an appropriate loader to handle this file type

reactjs - jsx 文件中的全局变量未加载

css - 有没有更好的方法来确保页脚停留在可滚动 div 的底部,其高度足以接触屏幕底部?

javascript - 无法使用 ThreeJS 和 React 导入 3D 模型

reactjs - React JS 卡住浏览器

javascript - 如何通过从 api 获取值来填充 react 选择的选项,以便在单击选择框时可见?