简体   繁体   English

this.refs =与babel env设置“最后2个chrome版本”反应时未定义

[英]this.refs = undefined in react with babel env settings “last 2 chrome versions”

I have a rather basic this.refs.form reference: 我有一个相当基本的this.refs.form参考:

someFunc(){
        console.info(this.refs.signInForm); //<form>
}

redner(){
    return (
        ...
        <form role="form" ref="signInForm" onSubmit={this.signInUser}>
        ...
    )
};

That works when my .babelrc file looks like this: 当我的.babelrc文件如下所示时,该方法有效:

{
  "plugins": [
    "transform-react-stateless-component-name",
    "transform-react-require", [
      "module-resolver", {
        "root": ["./src"],
        "alias": {
          "components": "./src/components",
          "configuration": "./configuration",
          "state": "./src/state",
          "test": "./src/test",
          "utils": "./src/utils"
        }
      }
    ],
    "transform-class-properties",
    "dynamic-import-node",
    "react-hot-loader/babel"
  ],
  "presets": [
    "env",
    "stage-0",
    "react"
  ]
}

However when I change the presents to: 但是,当我将礼物更改为:

  "presets":
  [
    ["env", {
      "targets": {
        "browsers": ["last 2 Chrome versions"]
      }
    }],
    "stage-0",
    "react"
  ]

this.refs becomes undefined. this.refs变得不确定。

I would like to be able to use env.targets.browsers and be able to use the refs in react. 我希望能够使用env.targets.browsers并能够在ref中使用引用。

You need to pass a callback function. 您需要传递一个回调函数。 See example from the docs https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element 请参阅docs https://reactjs.org/docs/refs-and-the-dom.html#adding-a-ref-to-a-dom-element中的示例

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

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