【问题标题】:Function components cannot have refs. Did you mean to use React.forwardRef()?函数组件不能有引用。你的意思是使用 React.forwardRef() 吗?
【发布时间】:2019-08-10 20:25:12
【问题描述】:

您好,我正在尝试从我的计算机中选择文件并在输入字段中显示文件名,但我收到此错误

函数组件不能有引用。你的意思是使用 React.forwardRef()

https://stackblitz.com/edit/react-aogwkt?file=bulk.js

这是我的代码

import React, { Component } from "react";
import {
  Button,
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
  FormControl,
  IconButton,
  Input,
  InputAdornment,
  withStyles
} from "@material-ui/core";
import Attachment from "@material-ui/icons/Attachment";
import CloudDownload from "@material-ui/icons/CloudDownload";
const BulkUpload = props => {
  const { classes } = props;
  return (
    <div className="App">
      <input
        id="file_input_file"
        className="none"
        type="file"
        ref={'abc'}
      />
      <Input
        id="adornment-attachment"
        type="text"
        fullWidth

        endAdornment={
          <InputAdornment position="end">
            <IconButton
              aria-label="Toggle password visibility"
              onClick={e => {
              // this.refs['abc'].click();
              }}
              className="login-container__passwordIcon"
            >
              <Attachment />
            </IconButton>
          </InputAdornment>
        }
      />
    </div>
  );
};

export default BulkUpload;

我只是想在输入字段中显示选定的文件名

【问题讨论】:

标签: javascript reactjs redux


【解决方案1】:

这里的问题是你使用了一个 非常 过时的使用 refs 的方法。你需要把你的代码改成这样的

const BulkUpload = props => {
  const { classes } = props;
  let inputRef = React.createRef();
  return (
    <div className="App">
      <input
        id="file_input_file"
        className="none"
        type="file"
        ref={inputRef}
      />
      <Input
        id="adornment-attachment"
        type="text"
        fullWidth

        endAdornment={
          <InputAdornment position="end">
            <IconButton
              aria-label="Toggle password visibility"
              onClick={e => {
                inputRef.current.click()
              }}
              className="login-container__passwordIcon"
            >
              <Attachment />
            </IconButton>
          </InputAdornment>
        }
      />
    </div>
  );
};

【讨论】:

    【解决方案2】:

    如果你使用 v16.8.0 或以上的 react,你可以使用 hooks useRef 方法来定义一个 ref 并使用它

    import React, { Component, useRef } from "react";
    import {
      Button,
      Dialog,
      DialogActions,
      DialogContent,
      DialogTitle,
      FormControl,
      IconButton,
      Input,
      InputAdornment,
      withStyles
    } from "@material-ui/core";
    import Attachment from "@material-ui/icons/Attachment";
    import CloudDownload from "@material-ui/icons/CloudDownload";
    const BulkUpload = props => {
      const { classes } = props;
      const inputRef = useRef(null);
      return (
        <div className="App">
          <input
            id="file_input_file"
            className="none"
            type="file"
            ref={inputRef }
          />
          <Input
            id="adornment-attachment"
            type="text"
            fullWidth
    
            endAdornment={
              <InputAdornment position="end">
                <IconButton
                  aria-label="Toggle password visibility"
                  onClick={e => {
                     inputRef.current.click();
                  }}
                  className="login-container__passwordIcon"
                >
                  <Attachment />
                </IconButton>
              </InputAdornment>
            }
          />
        </div>
      );
    };
    
    export default BulkUpload;
    

    如果您使用的是 v16.3.0 和 v16.8.0 之间的较低版本,您可以使用React.createRef

    const BulkUpload = props => {
      const { classes } = props;
      const inputRef = React.createRef(null);
      return (
        <div className="App">
          <input
            id="file_input_file"
            className="none"
            type="file"
            ref={inputRef}
          />
          <Input
            id="adornment-attachment"
            type="text"
            fullWidth
    
            endAdornment={
              <InputAdornment position="end">
                <IconButton
                  aria-label="Toggle password visibility"
                  onClick={e => {
                     inputRef.current.click();
                  }}
                  className="login-container__passwordIcon"
                >
                  <Attachment />
                </IconButton>
              </InputAdornment>
            }
          />
        </div>
      );
    };
    
    export default BulkUpload;
    

    否则,如果您使用的是更低的版本,那么您需要将您的组件转换为类组件并使用回调引用来使用引用,例如

    class BulkUpload extends Component {
       render() {
          const { classes } = this.props;
          return (
            <div className="App">
              <input
                id="file_input_file"
                className="none"
                type="file"
                ref={(ref) => this.inputRef = ref}
              />
              <Input
                id="adornment-attachment"
                type="text"
                fullWidth
    
                endAdornment={
                  <InputAdornment position="end">
                    <IconButton
                      aria-label="Toggle password visibility"
                      onClick={e => {
                         this.inputRef.click();
                      }}
                      className="login-container__passwordIcon"
                    >
                      <Attachment />
                    </IconButton>
                  </InputAdornment>
                }
              />
            </div>
          );
        };
    }
    
    export default BulkUpload;
    

    【讨论】:

    • 感谢您的回答。我使用的是 16.9,但使用的是用 16.3.1 编写的现有 React Native 代码。它使用带有ref=... 等的旧样式。我对钩子一无所知。这里最好的方法是什么?我应该像你上面那样阅读钩子并实现它们,还是有更简单的方法?
    【解决方案3】:
    const renderItem = ({ item, index }) => {
                return (
                <>          
                <Item
                    key={item.Id}
                    item={item}
                    index={index}
                />
                </>
            );
        };
    
    
       using fragment solve this issue 
        <> 
        </> 
    

    【讨论】:

    • 奇怪...但对我有帮助。谢谢你,凯沙夫!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 2021-08-24
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多