简体   繁体   English

如何解决 React 问题 (TypeError: Cannot read property 'showModel' of null)

[英]How solve React problem (TypeError: Cannot read property 'showModel' of null)

Here is show that how set attribute showModel and give it false :这里显示了如何设置属性showModel并给它false

Main.js主.js

import React from 'react'
import 'bootstrap/dist/css/bootstrap.min.css';
import Form from 'react-bootstrap/Form'
import Button from 'react-bootstrap/Button'
import UserDataModel from './UserDataModel';


class Main extends React.Component {

    constructor(props) {
        super(props);
        this.State = {
            userName: '',
            userHeight: '',
            likeCats: false,
            breed: '',
            showModel: false

        }

    }


    submitForm = async (event) => {
        event.preventDefault();
        let form = event.target;
        await this.setState({

            userName: event.target.userName.value,
            userHeight: event.target.userHeight.value,
            likeCats: event.target.likeCats.checked,
            breed: event.target.breed.value,
            showModel: true
        })
        form.reset();
        console.log(this.state.userHeight, "userNamennnnnnnnnnnnnnnnnnnnnnn");
    }
    handleClose = () => {
        this.setState({
            showModel: false
        })
    }

    render() {
        return (
            <div>
                <h2>Form popup</h2>
                <Form onSubmit={this.submitForm}>
                    <Form.Group className="mb-3" controlId="formBasicEmail">
                        <Form.Label>Name</Form.Label>
                        <Form.Control name="userName" type="text" placeholder="Enter Name" />

                        <Form.Label>Height</Form.Label>
                        <Form.Control type="text" name="userHeight" placeholder="Height" />

                    </Form.Group>


                    <Form.Group className="mb-3" controlId="formBasicCheckbox">
                        <Form.Check name="likeCats" type="checkbox" label="do you love food " />
                    </Form.Group>

                    <Form.Select name="breed" aria-label="Default select example">
                        <option>What is your favorate?</option>
                        <option value="1">Shawrma</option>
                        <option value="2">mansaf</option>
                        <option value="3">shorba</option>
                    </Form.Select>

                    <Button variant="primary" type="submit">
                        Submit
                    </Button>
                </Form>


                <UserDataModel
                    showModel={this.state.showModel}
                    handleClose={this.handleClose}
                    userName={this.state.userName}
                    userHeight={this.state.userHeight}
                    likeCats={this.state.likeCats}
                    breed={this.state.breed}
                />
            </div>
        )
    }
}

export default Main

UserDataModel.js用户数据模型.js

import React  from 'react'
import 'bootstrap/dist/css/bootstrap.min.css';
import Modal from 'react-bootstrap/Modal'
import Button from 'react-bootstrap/Button'

class UserDataModel extends React.Component {
   

    render() {
        return (
            <div>
             <Modal show={this.props.showModal} onHide={this.props.handleClose}>
                    <Modal.Header closeButton>
                        <Modal.Title>Modal heading</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                      
                        <p>{this.props.userName}</p>
                        <p>{this.props.userHeight}</p>
                        <p>{this.props.breed}</p>
                       
                
                    </Modal.Body>
                    <Modal.Footer>
                        <Button variant="secondary" onClick={this.props.handleclose}>
                            Close
                        </Button>
                    
                    </Modal.Footer>
                </Modal> 
            </div>
        )
    }
}

export default UserDataModel

In the constructor of your class component, you've used capital letter of State .在 class 组件的构造函数中,您使用了大写字母State Change it to small letter:改成小写:

constructor(props) {
    super(props);
    this.state = {
        userName: '',
        userHeight: '',
        likeCats: false,
        breed: '',
        showModel: false
    }
}

Also when you're setting the state , you should use spread javascript syntax for not changing the whole state. So change your setState to:此外,当您设置state时,您应该使用spread javascript语法来不更改整个 state。因此将您的setState更改为:

await this.setState({
    ...this.state,
    userName: event.target.userName.value,
    userHeight: event.target.userHeight.value,
    likeCats: event.target.likeCats.checked,
    breed: event.target.breed.value,
    showModel: true
})

And:和:

handleClose = () => {
    this.setState({
        ...this.state,
        showModel: false
    })
}

EDIT:编辑:

Another problem which exist in your code is that you've passed showModel in Main.js as props for the Modal component.您的代码中存在的另一个问题是您已经在Main.js中传递了showModel作为Modal组件的道具。 But in this line you've got showModal :但是在这一行中你有showModal

 <Modal show={this.props.showModal} onHide={this.props.handleClose}>

So change it to:所以将其更改为:

 <Modal show={this.props.showModel} onHide={this.props.handleClose}>

There is a difference between a and e ! ae是有区别的!

暂无
暂无

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

相关问题 如何在反应中解决“TypeError:无法读取未定义的属性'类别'”? - How to solve “TypeError: Cannot read property 'category' of undefined” in react? 如何解决未捕获的类型错误:无法读取 null 的属性“addEventListener”? - How to solve Uncaught TypeError: Cannot read property 'addEventListener' of null? 如何解决未捕获的typeError-无法读取null的属性&#39;addEventListener&#39; - how to solve uncaught typeError - cannot read property 'addEventListener' of null 如何解决“未捕获的TypeError:无法读取属性&#39;已检查&#39;的null” - How to solve the “Uncaught TypeError: Cannot read property 'checked' of null” 类型错误:无法读取 null 的属性“长度”,我该如何解决? - TypeError: Cannot read property 'length' of null, how can i solve it? TypeError: Cannot read property 'scrollIntoView' of null 如何解决? - TypeError: Cannot read property 'scrollIntoView' of null How to solve? 如何解决null的addEventListener属性无法读取的问题 - How to solve the problem of cannot read property addEventListener of null 如何在React中的Array中修复TypeError“无法读取null的属性&#39;x&#39;” - How to fix TypeError “cannot read property 'x' of null” at Array in React TypeError:无法在反应中读取 null 的属性“0” - TypeError: Cannot read property '0' of null in react TypeError:无法在 React 中读取 null 的属性“url” - TypeError: Cannot read property 'url' of null in React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM