简体   繁体   English

在严格模式下使用 UNSAFE_componentWillReceiveProps 的反应警告

[英]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.

相关问题 修复 'index.js:1 警告:不建议在严格模式下使用 UNSAFE_componentWillReceiveProps,这可能表明您的代码中存在错误' - Fixing 'index.js:1 Warning: Using UNSAFE_componentWillReceiveProps in strict mode is not recommended and may indicate bugs in your code' in react 使用React 16.3.2时未调用UNSAFE_componentWillReceiveProps - UNSAFE_componentWillReceiveProps not called when using React 16.3.2 React JS - ESLint - 使用 UNSAFE_componentWillMount、UNSAFE_componentWillUpdate 和 UNSAFE_componentWillReceiveProps 忽略组件的规则 - React JS - ESLint - Rules for ignoring components with UNSAFE_componentWillMount, UNSAFE_componentWillUpdate, and UNSAFE_componentWillReceiveProps 如何在不删除 react index.js 文件中的严格模式的情况下解决严格模式警告 - How can I solve strict mode warning without removing strict mode in react index.js file 使用电子商店时来自 ajv 的严格模式警告 - A strict mode warning from ajv when using electron-store 反应严格模式 - React Strict Mode 错误:使用反应时出现意外的严格模式保留字 - error: Unexpected strict mode reserved word when using react React 严格模式和双重渲染 - React strict mode and double rendering 反应:将setState与带有componentWillReceiveProps内部的函数一起使用 - React: Using `setState` with a function inside `componentWillReceiveProps` 严格模式与 React 18 的工作方式不同吗? - Does strict mode work differently with React 18?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM