2
我是React with Meteor的新手,我無法使用從已發佈集合中加載的現有數據填充表單。我成功發佈集合,但是當我嘗試訪問構造函數this.state
中的profileCandidate
時,表單將無法加載。有人能告訴我我在做什麼錯嗎?使用現有數據填充流星React表單
收藏:profileCandidate
{
"_id": "JGw6dTHG3RDjDQNXc",
"userId": "fYHKGTRhZvPKCETHQ",
"createdAt": "2017-04-25T12:05:30.449Z",
"name": {
"first": "John",
"last": "Doe"
}
}
組件:「ProfileCandidateForm.jsx`
class ProfileCandidateForm extends Component {
constructor(props) {
super(props);
this.state = {
firstName: [],
lastName: []
};
this.handleChange = this.handleChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
handleChange(event) {
const target = event.target;
const value = target.value;
const name = target.name;
this.setState({
[name]: value
});
}
handleSubmit(event) {
event.preventDefault();
profileCandidate = this.state;
Meteor.call('profileCandidate.insert', profileCandidate);
}
render() {
return (
<form onSubmit={this.handleSubmit.bind(this)}>
<label>
Name:
</label>
<input
type="text"
name="firstName"
value={this.state.firstName}
onChange={this.handleChange}
placeholder="First name"
/>
<input
type="text"
name="lastName"
value={this.state.lastName}
onChange={this.handleChange}
placeholder="Last name"
/>
<input
type="submit"
value="Submit"
/>
</form>
)
}
}
ProfileCandidateForm.propTypes = {
profileCandidate: PropTypes.object.isRequired,
}
export default createContainer(() => {
Meteor.subscribe('profileCandidate');
return {
profileCandidate: ProfileCandidate.findOne({userId: Meteor.userId()}),
};
}, ProfileCandidateForm);
你能CONSOLE.LOG'this.props'? –
我把'console.log(this.props)'放在'super(props)'下,並且它返回了'Object {profileCandidate:Array(0)}' – bp123
如果你在構造函數中使用console.log props我認爲這是正常的空導致組件尚未收到道具。你也期望集合ProfileCandidate返回一個空數組嗎? –