【发布时间】:2019-07-08 23:26:40
【问题描述】:
我试图在新笔记中显示带有表单的模式。 我将包含表单的 JSX 传递给模式,输入的 onChange 方法会触发但不会更新状态。我看到事件与 console.log 以及 name 和 value 的值一起触发,但状态没有更新。任何帮助将不胜感激。
Modal.js
import React from "react";
import { Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
export default function MyModal(props) {
return (
<Modal isOpen={props.isOpen}>
<ModalHeader>{props.modalTitle}</ModalHeader>
<ModalBody>{props.modalBody}</ModalBody>
<ModalFooter>
<Button color="primary" onClick={props.modalAction}>
Yes
</Button>{" "}
<Button color="secondary" onClick={props.handleModal}>
Cancel
</Button>
</ModalFooter>
</Modal>
);
}
LeadView.js
export class LeadView extends Component {
constructor() {
super()
this.state = {
result: '',
message: '',
note: '',
modal: false,
modalTitle: '',
modalBody: '',
modalAction: '',
}
}
handleChange = event => {
console.log('name', event.target.name)
console.log('value', event.target.value)
const { name, value } = event.target
this.setState({
[name]: value,
})
}
handleModalForNote = () => {
this.setState({
modal: !this.state.modal,
modalTitle: 'New Note',
modalBody: (
<div className="form-group">
<input
type="text"
className="form-control"
onChange={this.handleChange}
name="note"
value={this.state.note}
/>
</div>
),
modalAction: this.handleCallClick,
})
}
render() {
return (
<Fragment>
<MyModal
handleModal={this.handleModalClose}
modalAction={this.state.modalAction}
isOpen={this.state.modal}
modalBody={this.state.modalBody}
modalTitle={this.state.modalTitle}
modalOptions={this.state.modalOptions}
/>
<button onClick={this.handleModalForNote} className="btn btn-primary">
Write Note
</button>
</Fragment>
)
}
}
【问题讨论】:
标签: reactjs reactstrap