简体   繁体   English

组件没有在反应中定义?

[英]Component is not defined in react?

using login function as component throws an error like this: 'LoginModal' is not defined react/jsx-no-undef I dont know why this happening.使用登录功能作为组件会引发这样的错误:'LoginModal' 未定义 react/jsx-no-undef 我不知道为什么会发生这种情况。 Thanks in advance.提前致谢。

export default class Login extends Component {

    state={
        loading: false,
        modalShow: false,
        clicked: false,
        password:''
    }

    LoginModal = () => {
        return <Modal {...this.props} size="lg" aria-labelledby="contained-modal-title-vcenter">
              <Modal.Header closeButton>An otp has been sent to your Email!</Modal.Header>
                <p>something...</p>
                  <Button variant="outline-primary" onClick={this.setState({modalShow:false})} >Change Password</Button>  
            </Modal>
    }
    handleForgetPassword = () => {
        this.setState({modalShow: true})
    }

    handleSubmit = (event) => {
       .....
    }

    render() { 
        return (
            <div>
                <div id="login-wrapper">
                        <p onClick={this.handleForgetPassword} className="forgot-password">forgot password?</p>
                    </div>  
                </div>
                <LoginModal
                    show={this.state.modalShow}
                    onHide={() => this.setState({modalShow:false})}/>
            </div>
        )
    }
}

You should initialize the state in the constructor https://reactjs.org/docs/react-component.html#constructor您应该在构造函数中初始化状态https://reactjs.org/docs/react-component.html#constructor

  constructor(props) {
    super(props);
    this.state={
        loading: false,
        modalShow: false,
        clicked: false,
        password:''
    }

Also, fix the render method as it's having a missing , or an extra closing tag此外,修复 render 方法,因为它缺少 或额外的结束标记

render() { 
    return (
        <div>
            <div id="login-wrapper">
                    <p onClick={this.handleForgetPassword} className="forgot-password">forgot password?</p>
             </div>  
            <LoginModal
                show={this.state.modalShow}
                onHide={() => this.setState({modalShow:false})}/>
        </div>
    )
}

You're not referencing LoginModal correctly.您没有正确引用LoginModal You should do something like this instead:你应该做这样的事情:

Create a standalone component and then pass props to it创建一个独立的组件,然后将 props 传递给它

const LoginModal = ({onClick, ...props}) => {
        return <Modal {...props} size="lg" aria-labelledby="contained-modal-title-vcenter">
              <Modal.Header closeButton>An otp has been sent to your Email!</Modal.Header>
                <p>something...</p>
                  <Button variant="outline-primary" onClick={onClick} >Change Password</Button>  
            </Modal>
    }

Try this way试试这个方法

return (
        <div>
             ......
            {this.LoginModal()}
       </div>
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM