• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

제로초님..!

21.07.21 14:47 작성 조회수 209

0

TypeError: imageInput.current.click is not a function

에러가 나고 있어요.. 콘솔에 찍어봐도 click은 안보이고 검색해보고 비교도 해봤는데 도저히 못찾겠네요 ㅡㅠ  코드 한번만 봐주세욥..

import { Button, Form, Input } from "antd";
import React, { useCallback, useRef } from "react";
import { useDispatch, useSelector } from "react-redux";
import useInput from "../hooks/useInput";
import { addPost } from "../reducers/post";

const PostForm = () => {
  const dispatch = useDispatch();
  const imageInput = useRef();
  const [text, onChangeText, setText] = useInput("");
  const { imagePaths } = useSelector((state) => state.post);
  const onSubmit = useCallback(() => {
    dispatch(addPost);
    setText("");
  }, []);

  const onClickImageUpload = useCallback(() => {
    imageInput.current.click();
  }, [imageInput.current]);
  return (
    <Form
      style={{ margin: "10px 0 20px" }}
      encType="multipart/form-data"
      onFinish={onSubmit}
    >
      <Input.TextArea
        value={text}
        onChange={onChangeText}
        maxLength={140}
        placeholder="어떤 신기한 일이 있었나요?"
      />
      <div>
        <Input type="file" multiple hidden ref={imageInput} />
        <Button onClick={onClickImageUpload}>이미지 업로드</Button>
        <Button type="primary" style={{ float: "right" }} htmlType="submit">
          짹짹
        </Button>
      </div>
      <div>
        {imagePaths.map((v) => (
          <div key={v} style={{ display: "inline-block" }}>
            <img src={v} style={{ width: "200px" }} alt={v} />
            <Button>제거</Button>
          </div>
        ))}
      </div>
    </Form>
  );
};

export default PostForm;

답변 5

·

답변을 작성해보세요.

0

심스님의 프로필

심스

2021.12.01

저도 같은 에러에서 헤매다가 코드 수정했네요. 질문 감사드려요

0

YOUNG EUN님의 프로필

YOUNG EUN

질문자

2021.07.22

제로초님...! 죄송해요!

오타였어요 ㅜㅠ Input으로 했더라구요!

감사합니당!

0

YOUNG EUN님의 프로필

YOUNG EUN

질문자

2021.07.21

<Input type="file" multiple hidden innerRef={imageInput} />

이렇게 바꾸는거 맞죠?? 바꾸면 

react_devtools_backend.js:2574 Warning: React does not recognize the `innerRef` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `innerref` instead. If you accidentally passed it from a parent component, remove it from the DOM element.

이런 에러가 떠욥..!  그리고 콘솔로 imageInput.current 치면 null로나와요 ㅜㅠ

0

YOUNG EUN님의 프로필

YOUNG EUN

질문자

2021.07.21

이렇게 뜹니다..!ㅜㅠ

바꿨는데도 이렇게 뜬다구요??

YOUNG EUN님의 프로필

YOUNG EUN

질문자

2021.07.21

넵.... 

ref 대신 innerRef로 바꾸면 어떤가요?

0

imageInput.current?.click()으로 바꿔보세요.