2017-08-21 5 views
0

関数renderNotes()は、別のファイルにあるマップされた配列を返すことになっています。 JSXを返すと何も画面に戻らないことに気付きました。私はトラッカー機能に情報を戻している理由が分かっていると思います。 tracker.autorun()関数の内部で、どのように情報をrenderNotes()関数に戻すには?Meteor - 何も返さない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'が何をしているのか、そしてあなたが購読しているときに考える必要があります。あなたが持っているように、あなたは 'autorun'内の関数が呼び出されるたびに再度購読しています。 – chazsolo

+0

申し訳ありませんが、私は初心者です。あなたが購読し続ける必要がないので、tracker.autorun機能の外で購読をするとどうなるでしょうか?私が以前に求めていたリターンの問題をどうやってやりますか? –

+0

[流星群データを反応成分にするためのガイド](https://guide.meteor.com/react.html#using-createContainer)を読んでいますか?データとコンポーネントのライフサイクルを考えます。コンポーネントがマウントされると、一度購読します。アンマウントすると、購読を停止することができます。 'renderNotes'関数は、正しい要素をレンダリングするという1つのことしかしてはいけません。メソッドが呼び出されるまでに(そしてサブスクリプションのために再実行する)、コンポーネントに必要なデータが必要です。 – 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

私は実際にこれに非常に似た解決策を見つけました。私が答えを見つけたことを人々が知っているように –

関連する問題