[英]React warning for Using UNSAFE_componentWillReceiveProps in strict mode
I am very beginner in react and i got stacked with a warning, I can not resolve them even i read a lot about it in the internet.我是反应的初学者,我收到了警告,即使我在互联网上阅读了很多关于它的内容,我也无法解决它们。
The warning is:警告是:
The App.tsx relevant code parts: App.tsx 相关代码部分:
const [selectedMoment, setSelectedMoment] = useState<IMoment | null>(null);
const [editMode, setEditMode] = useState(false);
const handleOpenCreateForm = () => {
setSelectedMoment(null);
setEditMode(true);
}
return (
<Fragment>
<NavBar openCreateForm={handleOpenCreateForm} />
</Fragment>);
The menu is in the NavBar.tsx:菜单位于 NavBar.tsx 中:
interface IProps {
openCreateForm: () => void;
}
export const NavBar: React.FC<IProps> = ({ openCreateForm }) => {
return (
<Menu fixed='top' inverted>
<Container>
<Menu.Item>
<Button positive content="Moment upload" onClick={openCreateForm} />
</Menu.Item>
</Container>
</Menu>
)
}
They are semantic-ui-react elements.它们是语义-ui-react 元素。 Anybody idea why do i get this warning?
有人知道为什么我会收到此警告吗?
This method is considered legacy, the alternative API is getDerivedStateFromProps .此方法被认为是旧方法,替代 API 是getDerivedStateFromProps 。
Here's a sample of what the old method would look like:这是旧方法的示例:
class List extends React.Component {
componentWillReceiveProps(nextProps) {
if (nextProps.selected !== this.props.selected) {
this.setState({ selected: nextProps.selected });
this.selectNew();
}
}
// ...
}
The new method works a bit differently:新方法的工作方式有点不同:
class List extends React.Component {
static getDerivedStateFromProps(props, state) {
if (props.selected !== state.selected) {
return {
selected: props.selected,
};
}
// Return null if the state hasn't changed
return null;
}
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.