I have made a common component which served as a parent component.
I have made a PageCommonComponent as below.
import { Link } from 'react-router-dom';
import { Component } from 'react';
import ActionType from '../helper/Enum';
import axios from 'axios';
import BaseService from './base-service';
class PageCommon extends Component {
baseService = new BaseService();
constructor(pros) {
super(pros);
this.state = {
action: pros.action,
indexPageUrl: pros.indexPageUrl,
url: pros.url,
saveData: pros.saveData
}
}
saveData = () => {
console.log(this.state.saveData)
alert('save button clicked')
}
modifyData = () => {
}
deleteData = () => {
}
render() {
let button;
if (this.state.action === ActionType.Create) {
button =
<button type="button" onClick={this.saveData} className="btn btn-primary btn-round">
<i className="nc-icon nc-simple-add"></i>
Create
</button>
} else if (this.state.action === ActionType.Modify) {
button =
<button type="button" onClick={this.modifyData} className="btn btn-primary btn-round">
<i className="nc-icon nc-simple-update"></i>
Modify
</button>
} else if (this.state.action === ActionType.Delete) {
button =
<button type="button" onClick={this.deleteData} className="btn btn-danger btn-round">
<i className="nc-icon nc-simple-remove"></i>
Delete
</button>
}
return (
<div>
{button}
<Link className="btn btn-danger btn-round" to={this.state.indexPageUrl}>
<i className="nc-icon nc-simple-remove"></i>
Cancel
</Link>
</div>
);
}
}
export default PageCommon;
This component will done all of the crud functionality here. But the value will be passed from another child component call RoleCreate.Here is how I have used this component on another component.
import { Component } from 'react';
import PageCommon from './../../../common/pagecommon';
class RoleCreate extends Component {
constructor(pros) {
super(pros);
this.state = {
formData: {
roleName: ''
}
}
}
handleChange = (postData) => {
this.setState({ formData: postData });
}
render() {
return (
<div className="row">
<form style={{ 'width': '100%' }}>
<div className="col-md-12 col-sm-12 col-lg-12 card card-user">
<div className="row card-header" style={{ "borderBottom": "1px solid #d0c8c8" }}>
<div className="col-md-8">
<h5 className="card-title">Role Create</h5>
</div>
<div className="col-md-4 text-right">
<PageCommon
action={this.state.action}
url="/role/create"
indexPageUrl="/role"
saveData={this.state.formData}
onSubmitChild={this.state.formData} />
</div>
</div>
<div className="card-body">
<div className="row">
<div className="col-md-5 pr-1">
<div class
Name="form-group">
<label>Role Name</label>
<input type="text"
onChange={(e) => this.handleChange({ roleName: e.target.value })}
className="form-control" placeholder="Role Name" />
</div>
</div>
</div>
</div>
</div>
</form>
</div>
);
}
}
export default RoleCreate;
I have pass form data via saveData parameter but this doesn't passes the value instead only pass the object of that formData without any value.
How can I do this please help me. Thank you.
PageCommonaccesses any of the props passed to it after mounting. As-is,PageCommonis a child component ofRoleCreate.RoleCreatehas nothis.state.action. Did you really want to havePageCommonrenderRoleCreateas a child and pass a callback thatRoleCreatecould call and pass form data back out toPageCommon? That or you need to implement thecomponentDidUpdatemethod inPageCommon.saveData={this.state.formData}is used to pass formcontent to PageCommon component but it doesn't do as expected. Can i share a state value between components in react??PageCommonto do with updated props when they are recieved?