Skip to content

Commit

Permalink
Added the shouldComponentUpdate lifecycle and the mounting function
Browse files Browse the repository at this point in the history
  • Loading branch information
damiano-carradori committed Jan 20, 2019
1 parent 8ad9c18 commit 3a99e0f
Showing 1 changed file with 26 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/Form/FormContext.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
import React, {createContext, PureComponent} from 'react'
import React, {createContext, Component} from 'react'

export const FormContext = createContext(null);

export class FormContextProvider extends PureComponent {
export class FormContextProvider extends Component {
constructor(props) {
super(props);

this.onSubmit = this.onSubmit.bind(this);
this.onChange = this.onChange.bind(this);
this.onMount = this.onMount.bind(this);

this.state = {
onSubmit: this.onSubmit,
onChange: this.onChange,
fields: null,
onMount: this.onMount,
fields: {},
values: {}
}
}

shouldComponentUpdate(nextProps) {
const {onSubmit} = this.props;
return onSubmit !== nextProps.onSubmit;
}

onSubmit(event) {
event.preventDefault();
const {onSubmit} = this.props;
onSubmit(this.state.fields);
onSubmit(this.state.values);
}

onChange({target}) {
const {name, value} = target;

this.setState(({values}) => ({
values: {
...values,
[name]: value || undefined,
},
}));
}

onMount(field){
const {name} = field;

this.setState(({fields}) => ({
fields: {
...fields,
[name]: value || undefined
[name]: field,
}
}));
}
Expand Down

0 comments on commit 3a99e0f

Please sign in to comment.