ReactJS ES6导入类问题
ReactJS ES6 Issue with import class
在ReactJS中,我试图做一些非常简单的事情。我创建了一个类:
//SomeName.js
class SomeName {
constructor(name){
this.name = name;
}
}
在我的React.component中,我有:
//Index.js
import React from 'react'
import SomeName from './parts/SomeName'
class Index extends React.Component{
constructor(){
super();
let newName = new SomeName("John Doe");
this.getName = this.getName.bind(this);
}
getName(){
return newName;
}
render() {
return (
<div className="pages-container">
Hello {this.getName}
</div>
)
}
};
然而,我在Index.js构造函数中添加了一个调试器,没有引用SomeName。我看到的每一个引用都表明它是这样做的(但不是在ReactJS中),而且我在导入我制作的组件时没有问题,只有当我试图返回这个值时。我觉得我错过了一些非常简单的东西,我只是不知道是什么。有人能伸出援手吗?
1)将newName
定义为局部变量。要使其在getName
中可访问,应将其分配到this
:
constructor() {
super();
this.newName = new SomeName("John Doe"); // fixed here
this.getName = this.getName.bind(this);
}
getName(){
return this.newName; // and here
}
2) 您应该在render
方法中调用getName
。否则你会得到函数,而不是结果:
render() {
return (
<div className="pages-container">
Hello {this.getName()} //do not forget parentheses
</div>
)
}
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 如何使用url加载程序在webpack中导入多个图像
- 将PHP变量传递给jQuery时遇到问题
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 参数变量出现ngTable指令问题
- 剑道网格jQuery动画()问题
- 我的jQuery插件参数没有正确启动,遇到了问题
- 如何将JSON数据导入我的ejs模板
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- TableExport jquery插件:文件名和扩展名问题
- 在 JavaScript 中将值导入动态变量时出现问题
- 使用“需要导入粉尘.js”是否存在任何已知问题
- ReactJS ES6导入类问题
- JSLint-导入包时出现问题
- Polymer出现问题,无法识别我在导入文件中创建的类
- 将Materialize导入Aurelia项目时出现问题
- 转换angular 1的问题.将x控制器导入TypeScript
- 在FOR之外引用JSON导入对象时遇到问题
- InDesign启动脚本未从XML导入加载图像时出现问题
- 导入和导出时的基本Typescript应用程序问题