繁体   English   中英

使用Flowrouter和trackerreact显示来自Meteor.call函数的数据。

[英]Display data from Meteor.call function with Flowrouter and trackerreact.

我有这段代码,

import React from 'react';
import ReactDOM from 'react-dom';
import { Meteor } from 'meteor/meteor';
import TrackerReact from 'meteor/ultimatejs:tracker-react';
const username = "test";
export default class TasksWrapper extends TrackerReact(React.Component){
    constructor(){
        super();
        Meteor.call('getMyInfo', (error,data)=>{
            if(error){
                alert(error);
            }else{
                this.username = data.username;
            }
        });
    }
    render(){
        return (
        <div className="container">
        <div className="row profile">
            {this.username}
        </div></div>
        )
    }
}

上面的代码行不起作用。 我要在类名称为“ row profile”的div中显示用户的用户名。

当我运行这些代码时,我只会得到“测试”。 它显示在页面中。 我要显示的是usernae。 例如“约翰”。

我该怎么做? 有样品吗?

您需要按state存储username ,以便在username更改时,React将重新渲染组件:

export default class TasksWrapper extends TrackerReact(React.Component) {
  constructor() {
    super();
    this.state = {
      username: ''
    };
    Meteor.call('getMyInfo', (error, data) => {
      if (error) {
        alert(error);
      } else {
        this.setState({username: data.username});
      }
    });
  }
  render() {
    return (
      <div className="container">
        <div className="row profile">
          {this.state.username}
        </div>
      </div>
    )
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM