我正在尝试在Meteor + React中设置一个“编辑配置文件”功能。实际的更新工作正常,但是当我加载页面时,不会显示当前数据。几次console.log()调用显示它最初是未定义的,可能是在设置状态之后,稍后再正确更新,但是状态已经设置好了。
对于普通订阅,我将使用$ subReady-对自动发布的用户数据使用什么?
这是我的代码:
import React from 'react';
import { Meteor } from 'meteor/meteor';
import { withTracker } from 'meteor/react-meteor-data';
import { Card, Loader, Form } from 'semantic-ui-react';
// React component
class ProfileCard extends React.Component {
state = { username: this.props.currentUser ? this.props.currentUser.username : '' }
handleChange = (e, { name, value }) => this.setState({ [name]: value })
handleSubmit = () => {
const { username } = this.state
Meteor.call('user.update', username);
}
render() {
const { username } = this.state
return (
<Card fluid>
{ this.props.currentUser ? (
<Card.Content>
<Card.Header>Account Data</Card.Header>
<Form onSubmit={this.handleSubmit}>
<Form.Input
label="Username"
name="username"
type="text"
placeholder="set user name"
value={username}
onChange={this.handleChange}
/>
<Form.Button primary content="Submit" />
</Form>
</Card.Content>
) : (
<Card.Content>
<Loader />
</Card.Content>
)}
</Card>
);
}
}
export default withTracker(props => {
return {
currentUser: Meteor.user(),
};
})(ProfileCard);