【问题标题】:How can I solve strict mode warning without removing strict mode in react index.js file如何在不删除 react index.js 文件中的严格模式的情况下解决严格模式警告
【发布时间】:2020-11-11 08:23:58
【问题描述】:

当我在下面的表格中使用<Button /> 时,它会说

Warning: findDOMNode is deprecated in StrictMode. findDOMNode was passed an instance of RefFindNode which is inside StrictMode. Instead, add a ref directly to the element you want to reference.

即使我使用了 useRef,它仍然显示此警告。那么我可以在不删除索引文件中的 的情况下修复它

import React, {useRef} from 'react'
import { Button, Container, Form, Input } from 'semantic-ui-react'
import Layout from '../layout/Layout'

const Login = () => {

    let btn = useRef(null)
    React.useEffect(() => {
        console.log(btn);
    })

    return (
        <Layout>
            <Container className="auth-form segment">
                <Form>
                    <Form.Field 
                        label="Email"
                        control={Input}
                        placeholder="example@gmail.com"
                        name="email"
                    />
                    <Form.Field 
                        label="Password"
                        control={Input}
                        placeholder="Password"
                        name="password"
                        type="password"
                    />
                    <Button content="Login" ref={btn} primary/>
                </Form>
            </Container>
        </Layout>
    )
}

export default Login

【问题讨论】:

    标签: javascript reactjs strict-mode


    【解决方案1】:

    我认为你做不到。

    相反,你为什么不做类似的事情(在 index.js 文件中):

    if (process.env.NODE_ENV === 'development') {
       return <React.StrictMode><App /></React.StrictMode>;
    }
    else
    {
       return <App />;
    }
    

    您可以根据需要使用其他标志... 只是一个想法。

    【讨论】:

    • 那是为什么呢?在 StrictMode 中,我们可以看到潜在的警告。警告告诉我们更改代码以提高性能。不是吗?但是,为什么我们不能解决这个问题?
    猜你喜欢
    • 2021-03-09
    • 2020-06-13
    • 2020-08-22
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 2013-05-15
    • 2014-11-02
    • 2014-07-09
    相关资源
    最近更新 更多