简体   繁体   English

React + Meteor:this.props返回未定义

[英]React + Meteor: this.props is returning undefined

hello Idk why but i am trying to work with some static data and pass it around using props in my meteor and react project but cannot access the static data i created in the 'updates' variable.. can someone please help me understand why and fix this issue, thank you much appreciated:) 您好Idk为什么,但是我尝试使用一些静态数据,并在流星中使用props将其传递给React项目,但无法访问我在“ updates”变量中创建的静态数据。。有人可以帮助我了解原因并修复这个问题,非常感谢:)

import React, { Component } from 'react';
import { default as UpdateCard } from '../components/UpdateCard.jsx';

let updates = {
    title: 'Replace Title A with B',
    content: 'Lorem ipsum dolor sit amet enim. Etiam ullamcorper. Suspendisse a pellentesque dui, non felis. Maecenas malesuada elit lectus felis,   malesuada ultricies. Curabitur et ligula'
}

export default class UpdatesView extends Component {
    render() {
        console.log(this.props.updates);
        return (
            <div>
                <UpdateCard updates={this.props.updates}/>       
            </div>
        )
     }
}

your code is running as expected. 您的代码按预期运行。 the variable you created with a let keyword is not a prop, but just a local variable. 使用let关键字创建的变量不是prop,而是局部变量。 this.props.update s will return as undefined because this.props.update s将返回undefined,因为

  • updates prop was not passed into UpdatesView component 更新道具未传递到UpdatesView组件中
  • no default values for updates props were specified on UpdatesView 在UpdatesView上未指定更新道具的默认值

try adding the following below the UpdatesView class, it would add default props to your components. 尝试在UpdatesView类下面添加以下内容,它将为您的组件添加默认属性。

    UpdatesView.defaultProps = {
      updates:  {
           title: 'Replace Title A with B',
           content: 'Lorem ipsum dolor sit amet enim. Etiam '
       }
    }

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

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