gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 17:31:38 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com