reactjs - How to call a same modal with empty form what i have edited row data on a table row Click?
问题描述
i am getting data in antd table using json data.Now click on a table row iam getting the data in a Modal.Now i want to call same modal on click on register button with empty data?
Here my sample data for StudentTable
showModal = () => {
this.setState({
visible: true,
});
}
onOk = (e) => {
this.setState({
visible: false,
})
}
onCancel = (e) => {
console.log(e);
this.setState({
visible: false
})
}
rowClick = (record, rowIndex) => {
alert("student id...." + record.id)
alert(JSON.stringify(record))
this.showModal();
this.setState({
StudentData: record
})
}
render() {
return (
<div>
<div align="right">
<Button type="primary" onClick={this.showModal}>Register</Button>
</div>
<h2>Student Data</h2>
<Table
dataSource={data}
columns={this.state.columns}
pagination={{ pageSize: 5 }}
rowKey={record => record.id}
onRow={(record, rowIndex) => {
return {
onClick: (event) => this.rowClick(record, rowIndex)
}
}}
/>
<NewModal visible={this.state.visible}
onOk={this.onOk}
onCancel={this.onCancel}
StudentData={this.state.StudentData}
/>
</div>
)
}
i have a NewModal. iam getting the data on a table rowClick using mapPropsValues.Now i want to call same Modal with empty data?If i click on a table row call same modal i am getting the data?Now How to get empty form click on Register button using same modal?
my modal has following code?
import React from 'react'
import {
Modal
} from 'antd'
import FormikApollo from "./FormikForm"
class NewModal extends React.Component {
state = {
visible: this.props.visible
}
render() {
const { StudentData} = this.props
console.log(this.props.StudentData)
return (
<Modal
title="Basic Modal"
visible={this.props.visible}
onOk={this.props.onOk}
onCancel={this.props.onCancel}
onCreate={this.handleCreate}
>
<FormikApollo StudentData={this.props.StudentData}/>
</Modal>
)
}
}
export default NewModal
And iam getting the form data using formik with antd component.Now i want to call same modal what i get click on a table row with empty form click on a register button?
<Form onSubmit={handleSubmit}>
<Row gutter={4}>
<Col span={12} push={5}>
<Field
name="id"
label="StudentID"
placeholder="Enter a Id"
component={AntInput}
type="text"
formitemlayout={formItemLayout}
/>
<Button type="primary" htmlType="submit">Submit</Button>
</Col>
</Row>
</Form>
</div>
)
}
}
const FormikApp = (withFormik)({
mapPropsToValues: ({StudentData}) => ({
...(StudentData)
}),
validationSchema: Yup.object().shape({
username: Yup.string()
.min(3, "Username must be above 3 characters")
.required("Username is required"),
email: Yup.string()
.email("Invalid Email !!")
.required("Email is required"),
password: Yup.string()
.min(6, "Password must be above 6 characters")
.required("Password is required"),
dateofbirth: Yup.string().required("Date is required")
}),
handleSubmit(values,{resetForm}) {
alert(JSON.stringify(values))
console.log(values)
}
})(FormikApollo)
解决方案
如果您想再次渲染 Modal 组件,请单击按钮,我建议您在有条件的情况下渲染。
所以你可以有这样的状态
state={showModal: false}
或者state={isSubmitted: false}
然后在您的 jsx 代码中,您将状态设置为与先前状态相反的位置,以显示/不显示模态
<button onClick={e => this.setState(prevState => ({showModal: !prevState.showModal}))}>Submit </button>
{this.state.showModal &&
<Modal {...props} show={this.state.showModal} />
}
然后你可以将现有的道具传递给你的模态,或者如果你需要的话,可以传递给其他道具。然后你传入状态,它控制模式显示或不进入模式。
编辑:
进一步了解问题后,有两种方法可以解决此问题。
使用 Redux/Thunk 之类的中间件将数据/道具传输到远离原始组件的另一个组件。https://github.com/reduxjs/redux-thunk
重新定位你的 props,这样你就可以从多个地方将 props 传递给你的模态组件(这需要重组你的架构,因此我建议使用类似 redux.
推荐阅读
- python - 我用逗号分隔的打印列表在星号处有一个逗号
- android - 动态添加图片
- flutter - Flutter Back 导航和 Bloc 数据
- wordpress - Wordpress - 网站被黑,它正在重定向到一些恶意网站
- asp.net - 找不到路径“C:\Program Files\IIS Express\saveimg\newimg.jpg”的一部分
- google-cloud-platform - 谷歌云存储 cors 政策允许所有
- azure-api-management - 在运行时从 API 管理策略获取 Key Vault 机密值并将其传递到后端 URL 的脚本
- php - 想要从数据库中获取内容作为菜单项的链接,其中菜单项是使用 AngularJs(ng-repeat) 动态调用的
- php - 如何修复“未定义的属性:Mahasiswa::$form_validation”?
- c# - 在块中设置路径 ASP.NET CORE MVC 2.2 WEBPACK 4 HtmlWebpackPlugin