2017-08-21 42 views
0

函數renderNotes()應該返回在單獨文件中映射的數組。我注意到當我返回一些JSX時,沒有任何東西返回到屏幕上。我想我知道原因是因爲它將信息返回給跟蹤器功能。如何在tracker.autorun()函數內返回renderNotes()函數?流星 - 返回裏面的Tracker.autorun函數不返回任何東西

import { Meteor } from "meteor/meteor"; 
import React from "react"; 
import { withRouter, Link } from "react-router-dom"; 
import { Accounts } from "meteor/accounts-base"; 
import { Tracker } from "meteor/tracker"; 

import SubjectRoutes from "./subjectRoutes/subjectRoutes"; 
import { Notes } from "../methods/methods" 
import Menu from "./Menu.js"; 

class Home extends React.Component{ 
    componentWillMount() { 
    if(!Meteor.userId()){ 
     this.props.history.replace("/login") 
    } 
    } 
    logoutUser(e){ 
    e.preventDefault() 
    Accounts.logout(() => { 
     this.props.history.push("/login"); 
    }); 
    } 
    renderNotes(){ 
    Tracker.autorun(function() { 
     Meteor.subscribe('notes'); 
     let notes = Notes.find().fetch(); 
     // return notes.map((note) => { 
     // return <p>{note.imageURL}</p> 
     // }) 
     return <p>asdas</p> //<--Here 
    }); 
    } 
    render(){ 
    return (
     <div> 
     <button onClick={this.logoutUser.bind(this)}>Logout</button> 
     {this.renderNotes()} 
     <Menu /> 
     </div> 
    ) 
    } 
} 

export default withRouter(Home); 
+0

只需一眼,你需要考慮'renderNotes'正在做什麼,並且_when_您正在訂閱。如你所知,每次調用'autorun'內的函數時都會重新訂閱。 – chazsolo

+0

對不起,我是一名初學者,在將tracker.autorun函數以外的訂閱放入其中之後會有什麼結果,因爲您無需繼續訂閱,所以速度更快?以及我將如何處理我之前詢問的退貨問題? –

+0

您是否閱讀過[將Meteor數據導入React組件的指南](https://guide.meteor.com/react.html#using-createContainer)?考慮數據和組件生命週期。您在組件掛載時訂閱一次,(可能)在卸載時取消訂閱。 'renderNotes'函數應該只做一件事:渲染正確的元素。當該方法被調用(並且由於訂閱而重新運行)時,組件應該擁有它所需的數據。 – chazsolo

回答

0

這爲我工作:

import { Meteor } from "meteor/meteor"; 
import React from "react"; 
import { withRouter, Link } from "react-router-dom"; 
import { Accounts } from "meteor/accounts-base"; 
import { Tracker } from "meteor/tracker"; 

import SubjectRoutes from "./subjectRoutes/subjectRoutes"; 
import { Notes } from "../methods/methods" 
import Menu from "./Menu.js"; 

class Home extends React.Component{ 
    constructor(props){ 
    super(props) 
    this.state = { 
     notes: [] 
    } 
    } 
    componentWillMount() { 
    if(!Meteor.userId()){ 
     this.props.history.replace("/login") 
    } 
    this.tracker = Tracker.autorun(()=>{ 
     Meteor.subscribe('notes'); 
     let notes = Notes.find().fetch(); 
     this.setState({ notes }) 
    }); 
    } 
    componentWillUnmount() { 
    this.tracker.stop(); 
    } 
    logoutUser(e){ 
    e.preventDefault() 
    Accounts.logout(() => { 
     this.props.history.push("/login"); 
    }); 
    } 
    renderNotes(notes){ 
    return notes.map((note) => { 
     return (
     <div key={note._id}> 
      <img src={note.imageURL} /> 
      <p>{note.type}</p> 
     </div> 
    ) 
    }); 
    } 
    render(){ 
    return (
     <div> 
     <button onClick={this.logoutUser.bind(this)}>Logout</button> 
     <Menu /> 
     {this.renderNotes(this.state.notes)} 
     </div> 
    ) 
    } 
} 

export default withRouter(Home); 
0

不知道這是否是正確的答案,但我喜歡平時做這樣的事情

import TrackerReact from 'meteor/ultimatejs:tracker-react'; 
    import { Notes } from "../methods/methods"; 

    export default class Home extends TrackerReact(React.Component) { 
     constructor(props,) { 
     super(props); 
     this.state = { 
     subscription:{ 
      publishNotes: Meteor.subscribe("publish-Notes") 
     } 
     }; 
     } 
     returnNotes(){ 
     return Notes.find().fetch(); 
     } 

     render(){ 
     ... 

     const stuff = this.returnNotes().map((note)=>{ 
     return <p>{note}</p> 
     }); 

     return (
     .... 
     {stuff} 
     ) 
     } 
    } 
+0

我其實已經找到了一個解決方案,非常類似於這個,我會發布它並標記爲已回答,以便人們知道我已找到答案 –