Ala*_*uza 1 javascript mixins node.js reactjs
我试图从getDefaultProps访问mixin中定义的函数,但我得到了undefined is not a function.基本上我正在使用react-intl,我的列的默认标签应该被翻译.为什么我无法从getDefaultProps中访问mixin中定义的函数?
var React = require('react');
var ReactIntl = require('react-intl');
var IntlMixin = ReactIntl.IntlMixin;
var Index = React.createClass({
mixins: [IntlMixin],
getDefaultProps: function () {
return ({
options: {
attributes: [{name: 'name', label: this.getIntlMessage('Index.name'), index: 0}],
view: "tiles"
}
});
},
render: function() {
return <div>{this.props.options.attributes[0].label}</div>
}
});
module.exports = Index;
Run Code Online (Sandbox Code Playgroud)
它无法访问,因为getDefaultProps只调用一次而不是在您正在创建的类的实例的上下文中.的this背景下是不正确的.
您需要将检索移动到实例函数中,例如render.
您还应该知道返回的数组或对象实例getDefaultProps在所有实例之间共享.我不知道你是如何使用它的,但它可能会导致问题,具体取决于你如何使用这些值.
| 归档时间: |
|
| 查看次数: |
1192 次 |
| 最近记录: |