简体   繁体   English

React JS 组件在 Meteor 中多次渲染

[英]React JS component renders multiple times in Meteor

I using Meteor 1.3 for this application together with react js and Tracker React.我将 Meteor 1.3 与 react js 和 Tracker React 一起用于此应用程序。 I have a page to view all available users in the application.我有一个页面可以查看应用程序中的所有可用用户。 This page require user to login to view the data.此页面需要用户登录才能查看数据。 If user not logged in, it will shows the login form and once logged-in the component will render the user's data.如果用户未登录,它将显示登录表单,一旦登录,该组件将呈现用户的数据。

Main component for the logic.逻辑的主要组件。

export default class MainLayout extends TrackerReact(React.Component) {
    
    isLogin() {
        return Meteor.userId() ? true : false
    }
    render() {
    
        if(!this.isLogin()){
            return (<Login />)
        }else{
        return (
            <div className="container">
                <AllUserdata  />            
            </div>
          )
        }
    }
}

And in the AllUserdata component:AllUserdata组件中:

export default class Users extends TrackerReact(React.Component) {

    constructor() {
        super();

        this.state ={
            subscription: {
                Allusers : Meteor.subscribe("AllUsers")
            }
        }

    }

    componentWillUnmount(){
        this.state.subscription.Allusers.stop();
    }

    allusers() {
        return Meteor.users.find().fetch();
    }

    render() {
        console.log('User objects ' + this.allusers());
        return (
                <div className="row">
                    {
                    this.allusers().map( (user, index)=> {
                            return <UserSinlge key={user._id} user={user} index={index + 1}/>
                            })
                     }

                        
                </div>
                
            )
    }   
};

The problem is when logged in, it only shows the current user's data.问题是登录时,它只显示当前用户的数据。 All other user objects are not rendered.不呈现所有其他用户对象。 If I check on the console, console.log('User objects ' + this.allusers());如果我检查控制台, console.log('User objects ' + this.allusers()); show objects being rendered 3 times: the first render only shows the current user's data, the second one renders data for all users (the desired result), and the third one again renders only the current user's data.显示对象被渲染 3 次:第一次渲染只显示当前用户的数据,第二次渲染所有用户的数据(期望的结果),第三次再次只渲染当前用户的数据。

If I refresh the page, the user data will be rendered properly.如果我刷新页面,用户数据将正确呈现。

Any idea why?知道为什么吗?

React calls the render() method of components many times when it's running. React 在运行时会多次调用组件的render()方法。 If you're experiencing unexpected calls, it's usually the case that something is triggering changes to your component and initiating a re-render.如果您遇到意外调用,通常是因为某些东西触发了对组件的更改并启动了重新渲染。 It seems like something might be overwriting the call to Meteor.users.find().fetch() , which is probably happening because you're calling that function on each render.似乎有些东西可能会覆盖对Meteor.users.find().fetch()的调用,这可能是因为您在每次渲染时调用该函数。 Try inspecting the value outside of the render method or, better yet, rely on tests to ensure that your component is doing what it should be :)尝试检查 render 方法之外的值,或者更好的是,依靠测试来确保您的组件正在做它应该做的事情:)

From https://facebook.github.io/react/docs/component-specs.html#render来自https://facebook.github.io/react/docs/component-specs.html#render

The render() function should be pure, meaning that it does not modify component state, it returns the same result each time it's invoked, and it does not read from or write to the DOM or otherwise interact with the browser (eg, by using setTimeout). render() 函数应该是纯函数,这意味着它不修改组件状态,每次调用时都返回相同的结果,并且不读取或写入 DOM 或以其他方式与浏览器交互(例如,通过使用设置超时)。 If you need to interact with the browser, perform your work in componentDidMount() or the other lifecycle methods instead.如果您需要与浏览器交互,请改为在 componentDidMount() 或其他生命周期方法中执行您的工作。 Keeping render() pure makes server rendering more practical and makes components easier to think about.保持 render() 纯正使服务器渲染更实用,并使组件更易于思考。

See also:也可以看看:

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

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