contextTypes was defined as an instance property on MyButton. Use a static property to define contex

本文主要是介绍contextTypes was defined as an instance property on MyButton. Use a static property to define contex,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题描述
:在使用es6语法时,定义一个类方法时,出现如下的问题:

contextTypes was defined as an instance property on MyButton. Use a static property to define contextTypes instead.

出现上面问题的原因是,在es6版本中,对一些语法进行了更改,更改的内容中都有以下属性的修改:
将propTypes、getDefaultTypes等类属性移到类外面定义,由于ES6类中只允许定义方法并不允许定义类属性,所以像原先会在 createClass 中定义的 propTypes 、 getDefaultTypes 、 displayName 还有 contextTypes 等组件属性都要放到类外面来赋值。
也就是具体如下实现:
原来的方式:

var MyComponent = React.createClass({  displayName:'MyComponent',propTypes: {prop1: React.PropTypes.string},getDefaultProps: function() {return { prop1: '' };}
});

ES6的方式:

class MyComponent extends React.Component {...}  
MyComponent.displayName = 'MyComponent';  
MyComponent.propTypes = {  prop1: React.PropTypes.string
}
MyComponent.defaultProps = {  return { prop1: '' };
}

解决方案
:将使用的
contextTypes属性放置到class类的外边,然后重新编译调用即可,然后使用组件名进行调用,形式如下面代码所示:

class MyButton extends React.Component{constructor(props,context){super(props,context);this.handleSubmit=this.handleSubmit.bind(this);}// contextTypes: {//     router: React.PropTypes.object// }//...上面的contextTypes等属性的形式是错的,由于ES6类中只允许定义方法并不允许定义类属性
}
MyButton.contextTypes= {router: React.PropTypes.object
}

上面就是解决办法

这篇关于contextTypes was defined as an instance property on MyButton. Use a static property to define contex的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/753707

相关文章

native和static native区别

本文基于Hello JNI  如有疑惑,请看之前几篇文章。 native 与 static native java中 public native String helloJni();public native static String helloJniStatic();1212 JNI中 JNIEXPORT jstring JNICALL Java_com_test_g

Unable to instantiate Action, goodsTypeAction, defined for 'goodsType_findAdvanced' in namespace '/

报错: Unable to instantiate Action, goodsTypeAction,  defined for 'goodsType_findAdvanced' in namespace '/'goodsTypeAction......... Caused by: java.lang.ClassNotFoundException: goodsTypeAction.......

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef‘ of undefined“

vue 父组件调用子组件的方法报错,“TypeError: Cannot read property ‘subDialogRef’ of undefined” 最近用vue做的一个界面,引入了一个子组件,在父组件中调用子组件的方法时,报错提示: [Vue warn]: Error in v-on handler: “TypeError: Cannot read property ‘methods

OpenStack实例操作选项解释:启动和停止instance实例

关于启动和停止OpenStack实例 如果你想要启动和停止OpenStack实例时,有四种方法可以考虑。 管理员可以暂停、挂起、搁置、停止OpenStack 的计算实例。但是这些方法之间有什么不同之处? 目录 关于启动和停止OpenStack实例1.暂停和取消暂停实例2.挂起和恢复实例3.搁置(废弃)实例和取消废弃实例4.停止(删除)实例 1.暂停和取消暂停实例

Cannot read property ‘length‘ of null while opening vscode terminal

同一问题地址:Cannot read property ‘length’ of null while opening vscode terminal 问题描述 One day, 我在ubuntu 18.04下用vscode打开一个项目,并想和往常一样在vscode使用终端,发现报错Cannot read property 'length' of null。 解决 打开setting.jso

解决The valid characters are defined in RFC 7230 and RFC 3986

解决方法: 一、更换低版本的Tomcat;(我选的方案) 二、参考:https://blog.csdn.net/qq_32365919/article/details/82055800

Android 属性动画(Property Animation)

本文是学习以下三位大神之后,整理的学习笔记,彩蛋在编号6          http://blog.csdn.net/lmj623565791/article/details/38067475          http://www.cnblogs.com/angeldevil/archive/2011/12/02/2271096.html          http://www.tu

java基础总结08-面向对象4(static关键字)

原来一个类里面的成员变量,每new一个对象,这个对象就有一份自己的成员变量,因为这些成员变量都不是静态成员变量。对于static成员变量来说,这个成员变量只有一份,而且这一份是这个类所有的对象共享。 静态成员变量与非静态成员变量的区别 以下面的例子为例说明 package cn.galc.test;public class Cat {/*** 静态成员变量*/private static

Flask 创建app 时候传入的 static_folder 和 static_url_path参数理解

Flask 在创建app的时候 是用 app = Flask(__name__) 来创建的,不传入 static_folder参数的话 ,默认的静态文件的位置是在 static目录下 我们可以进入 Flask的源码里面查看 ctrl+鼠标左键进入 这是Flask的 __init__源码(后面还有一些,我就选了需要的代码)     def __init__(self,import_

Tomcat启动报错:transport error 202: bind failed: Address already in use

Tomcat启动报错:transport error 202: bind failed: Address already in use 了,上网查找了下面这篇文章。也是一种解决办法。 下文来自:http://blog.csdn.net/sam031503/article/details/7037033 tomcat 启动日志报出以下错误:  ERROR: transport err