Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 绑定";这";指向对象中定义的函数_Javascript_Reactjs_Redux_Redux Form - Fatal编程技术网

Javascript 绑定";这";指向对象中定义的函数

Javascript 绑定";这";指向对象中定义的函数,javascript,reactjs,redux,redux-form,Javascript,Reactjs,Redux,Redux Form,在我的React项目中,我创建了一个实用程序函数,用于呈现不同redux表单的字段: export const renderField = function(fieldConfig, fieldName) { //fieldHelper is provided by redux-form const fieldHelper = this.props.fields[fieldName]; return ( <div> <label>{fiel

在我的React项目中,我创建了一个实用程序函数,用于呈现不同redux表单的字段:

export const renderField = function(fieldConfig, fieldName) {
  //fieldHelper is provided by redux-form
  const fieldHelper = this.props.fields[fieldName];
  return (
    <div>
      <label>{fieldConfig.label}</label>
      <fieldConfig.fieldType
        {...fieldHelper}
        {...fieldConfig.props}
      />
    </div>
  );
}
export const renderField=函数(fieldConfig,fieldName){
//fieldHelper由redux表单提供
const fieldHelper=this.props.fields[fieldName];
返回(
{fieldConfig.label}
);
}
在其中一种形式中,我有一个json配置(字段)要传递给此函数:

const FIELDS = {
  name: {
    validate: validateName,
    fieldType: 'input',
    props: {
      className: 'name-field',
      type: 'text'
    }
  },
  description: {
    validate: validateDescription,
    fieldType: 'textarea',
    props: {
      className: 'desc-field',
      rows: 2,
      value: 'Add Description To Your Group',
      onBlur: this._hello // <--- ????????
    }
  }
}

class ShowGroup extends Component {
  _hello = () => {
    console.log(this); // this returns null or undefined
  };
  render() {
    return (
      <div className="show-group">
        <form>
          {_.map(FIELDS, renderField.bind(this))}
        </form>
      </div>
    );
  }
}

export default reduxForm({
  validate,
  //a unique id for this form
  form:'ShowGroup',
  fields: _.keys(FIELDS),
  fields_def: FIELDS
})(
  connect(null, {})(ShowGroup)
);
const字段={
姓名:{
验证:验证名称,
fieldType:'输入',
道具:{
className:“名称字段”,
键入:“文本”
}
},
说明:{
验证:验证描述,
字段类型:“textarea”,
道具:{
类名:“描述字段”,
行:2,
值:“向您的组添加说明”,
onBlur:这个。_你好//{
console.log(this);//返回null或未定义
};
render(){
返回(
{{{.map(FIELDS,renderField.bind(this))}
);
}
}
导出默认reduxForm({
验证
//此表单的唯一id
表格:'ShowGroup',
字段:u.键(字段),
字段定义:字段
})(
连接(空,{})(ShowGroup)
);
我想将
ShowGroup
组件绑定到
onBlur
回调函数。我该怎么做

我将
字段
移动到构造函数中,但在
\u hello
函数中仍然没有得到所需的

我知道
字段是在类之外定义的,这是我问题的重点:我想知道如何将类的上下文附加到该函数。如果绝对不可能,我怎么做?

您不能
绑定()
和arrow函数

两个因素影响了箭头函数的引入:短 本协议的功能和非约束性


函数
声明,而不是像
hello=function(){…}
一样,不能有绑定和箭头函数:

将您的
\u hello
更改为:

_hello(){
    console.log(this); // this returns null or undefined
  };
将字段移动到类中,并在将指定给
onBlur
的同时。 做一个绑定


onBlur:this.\u hello.bind(this)

看起来
字段
在类外。
this
始终指的是您所处的当前上下文,无论是类、函数还是全局上下文。其中全局
this
将与我提到的
窗口
相同,即使我将其移动到类内(对构造函数)对于
这个
我得到
null
/
未定义的
。Bind不适用于箭头函数。只是想知道你打算如何将参数传递给
renderField
,该参数由
redux form
处理(查看代码底部)。问题是,然后我丢失了指向字段的指针(看看代码的底部,我需要能够从类外指向字段)在这种情况下,让它留在类外,但是在调用
onBlur
时。从
renderFields
内部调用
fieldConfig.props.onBlur.call(this)
我隐式传递回调函数:
{…fieldConfig.props}