流星withTracker嵌套组件

时间:2018-12-06 12:53:08

标签: meteor

嗨,我有这样的代码:

class PlayCard extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    return (
      <Card.Group>
        {this.props.players.map(player => {
          return (
            <Card key={player._id}>
              <Card.Content>
                <Card.Header>Name: {player.name}</Card.Header>
                <Card.Description>
                  <List divided relaxed>
                    <List.Item>
                      <List.Content>
                        <List.Header> Points : {player.points}</List.Header>
                      </List.Content>
                    </List.Item>
                    <List.Item>
                      <List.Content>
                        <List.Header> Turn : {player.turn}</List.Header>
                      </List.Content>
                    </List.Item>
                  </List>
                </Card.Description>
              </Card.Content>
            </Card>
          );
        })}
      </Card.Group>
    );
  }
}

const PlayCardWithTracker = withTracker(props => {
  const play = props.play;
  return {
    players: Players.find({ _id: { $in: play.players } });,
  };
})(PlayCard);

class TrainerDashboard extends React.Component {
  constructor(props) {
    super(props);
  }
  render() {
    if (this.props.ready) {
      return (
        <div>
          {this.props.plays.map(play => (
            <PlayCardWithTracker key={play._id} play={play} />
          ))}
          <NewGameModal />
        </div>
      );
    } else {
      return <div />;
    }
  }
}

export default withTracker(props => {
  const handle = Meteor.subscribe("trainerCurrentPlays");
  return {
    ready: handle.ready(),
    plays: Plays.find({
      userId: Meteor.userId()
    }).fetch()
  };
})(TrainerDashboard);

问题是Players集合上的更新未出现在UI中。 PlayCard组件未重新呈现。我猜我用的是withTracker错误的方式。现在,我必须刷新页面以查看更改的传播,这绝对不是预期的行为。可以肯定的是我的代码有问题。任何帮助将不胜感激。

0 个答案:

没有答案