How solve React problem (TypeError: Cannot read property 'showModel' of null)
How solve React problem (TypeError: Cannot read property 'showModel' of null)
此处显示如何设置属性 showModel
并赋予它 false
:
Main.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
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
在 class 组件的构造函数中,您使用了 State
的大写字母。改成小写:
constructor(props) {
super(props);
this.state = {
userName: '',
userHeight: '',
likeCats: false,
breed: '',
showModel: false
}
}
此外,当您设置 state
时,您应该使用 spread javascript 语法来不改变整个状态。因此,将 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
})
并且:
handleClose = () => {
this.setState({
...this.state,
showModel: false
})
}
编辑:
您的代码中存在的另一个问题是您在 Main.js
中传递了 showModel
作为 Modal
组件的属性。但是在这一行中你有 showModal
:
<Modal show={this.props.showModal} onHide={this.props.handleClose}>
因此将其更改为:
<Modal show={this.props.showModel} onHide={this.props.handleClose}>
a
和e
有区别!
此处显示如何设置属性 showModel
并赋予它 false
:
Main.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
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
在 class 组件的构造函数中,您使用了 State
的大写字母。改成小写:
constructor(props) {
super(props);
this.state = {
userName: '',
userHeight: '',
likeCats: false,
breed: '',
showModel: false
}
}
此外,当您设置 state
时,您应该使用 spread javascript 语法来不改变整个状态。因此,将 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
})
并且:
handleClose = () => {
this.setState({
...this.state,
showModel: false
})
}
编辑:
您的代码中存在的另一个问题是您在 Main.js
中传递了 showModel
作为 Modal
组件的属性。但是在这一行中你有 showModal
:
<Modal show={this.props.showModal} onHide={this.props.handleClose}>
因此将其更改为:
<Modal show={this.props.showModel} onHide={this.props.handleClose}>
a
和e
有区别!