【发布时间】:2017-01-24 01:27:45
【问题描述】:
我正在使用 React 构建一个页面,我有两个组件,其中一个具有不同的功能。第一个,ProfileFill,捕获表单数据,第二个,ProfileFillPercent,在另一个文件中,对表单填充进行平均。
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
}
});
我正在创建一个变量,这是ProfileFill 的一个元素,我需要将它传递给另一个文件中的 ProfileFillPercent。我试图用单例模式传递它,比如this Stack explanation,但是,它不起作用。知道如何传达这两个不是父母但共享相同数据的组件吗?
在这种情况下,xpto 是存储 ProfileFill 状态的数据。
【问题讨论】:
标签: javascript reactjs