I am building a page with React and I this have two Components, which one has a different function. First, ProfileFill
, catching the form data and the second, ProfileFillPercent
, that is in another file and makes an average of the form fill.
ProfileFill.js:
import React from 'react';
import ReactDOM from 'react-dom';
export const CustomerFill = React.createClass({
handleChange() {
const customerName = this.customerName.value;
const customerCPF = this.customerCPF.value;
this.props.onUserInput(customerName, customerCPF);
},
render(){
const {customerName, customerCPF} = this.props;
return(
<div>
<div className="form-group col-sm-12 col-md-5">
<label htmlFor="inputName">Nome do segurado:</label>
<input
ref={(r) => this.customerName = r}
type="text"
className="form-control"
placeholder="Insira o nome do segurado"
value={customerName}
onChange={this.handleChange}
/>
</div>
<div className="form-group col-sm-12 col-md-5">
<label htmlFor="inputName">CPF do segurado:</label>
<input
ref={(r) => this.customerCPF = r}
type="number"
className="form-control"
placeholder="Insira o CPF do segurado"
value={customerCPF}
onChange={this.handleChange}
/>
</div>
</div>
)
}
});
export const ProfileFill = React.createClass({
getInitialState() {
return{
customerName: '',
customerCPF: '',
};
},
handleUserInput(customerName, customerCPF) {
this.setState({
customerName: customerName,
customerCPF: customerCPF,
});
},
render(){
const { customerName, customerCPF } = this.state;
this.xpto = this.state;
console.log(this.xpto);
return(
<div>
<div className="lateral-margin">
<h2>INFORMAÇÕES PESSOAIS</h2>
</div>
<div id="profile-fill" className="intern-space">
<form id="form-space">
<CustomerFill
customerName={customerName}
customerCPF={customerCPF}
onUserInput={this.handleUserInput}
/>
</form>
</div>
</div>
);
}
});
ReactDOM.render(
<ProfileFill />,
document.getElementById('fill-container')
);
export default ProfileFill;
ProfileFillPercent.js:
import React from 'react';
import ReactDOM from 'react-dom';
import ProfileFill from './profileFill.js';
console.log(ProfileFill.xpto);
export const ProfileFillPercent = React.createClass({
render() {
//things happen here
}
});
I am creating a variable to, this an element of ProfileFill
and I need passing it to ProfileFillPercent that is in another file. I am tried to pass it with singleton pattern, like this Stack explanation, however, it is not working. Any idea how can I communicate this both components which are not parents, but share the same single data?
In this case, xpto is the data that stored the state of ProfileFill.