簡體   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