简体   繁体   English

TypeError:使用“useRef”时无法读取未定义的属性“onMonthSelect”

[英]TypeError: Cannot read property 'onMonthSelect' of undefined while using "useRef"

i am trying to use useRef in my react functional component, but when i try to access it I am getting error "TypeError: Cannot read property 'onMonthSelect' of undefined while using "useRef" ".我正在尝试在我的反应功能组件中使用 useRef,但是当我尝试访问它时,我收到错误“TypeError:使用“useRef”时无法读取未定义的属性“onMonthSelect””。

Here is the code below for this这是下面的代码

import React, { useRef, useState, useEffect } from "react";
import moment from "moment";
import "react-dates/initialize";
import "react-dates/lib/css/_datepicker.css";
import { SingleDatePicker } from "react-dates";

const SingleDatePickerComponent = () => {
  const monthController = useRef();
  const [createdAt, setCreatedAt] = useState(moment());

  const onDateChange = (createdAt) => {
    console.log(createdAt);
    setCreatedAt(createdAt);
  };

  useEffect(() => {
    console.log(monthController);
    // TODO: check if month is visible before moving
    monthController.current.onMonthSelect(
      monthController.current.month,
      createdAt.format("M")
    );
//In this useEffect i am getting the error
  }, [createdAt]);

  return (
    <div>
      <div style={{ marginLeft: "200px" }}>
      </div>
      <SingleDatePicker
        date={createdAt}
        startDateId="MyDatePicker"
        onDateChange={onDateChange}
        renderMonthElement={(...args) => {
          // console.log(args)
          monthController.current = {
            month: args[0].month,
            onMonthSelect: args[0].onMonthSelect,
          };
          // console.log(monthController)
          return args[0].month.format("MMMM");
        }}
        id="SDP"
      />
    </div>
  );
};

export default SingleDatePickerComponent;

The ref value won't be set yet on the initial render. ref 值不会在初始渲染时设置。 Use a guard clause or Optional Chaining operator on the access.在访问上使用保护子句或可选链接运算符。

useEffect(() => {
  // TODO: check if month is visible before moving
  monthController.current && monthController.current.onMonthSelect(
    monthController.current.month,
    createdAt.format("M")
  );
}, [createdAt]);

or或者

useEffect(() => {
  // TODO: check if month is visible before moving
  monthController.current?.onMonthSelect(
    monthController.current.month,
    createdAt.format("M")
  );
}, [createdAt]);

It may also help to provide a defined initial ref value.它也可能有助于提供定义的初始参考值。

const monthController = useRef({
  onMonthSelect: () => {},
});

暂无
暂无

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

相关问题 未捕获的类型错误:无法使用 React 和 useRef 读取未定义的属性 - Uncaught TypeError: Cannot read properties of undefined using React and useRef 使用 useeffect 和 useref TypeError 时出错:无法读取 null 的属性“getBoundingClientRect” - Error using useeffect and useref TypeError: Cannot read property 'getBoundingClientRect' of null 类型错误:使用反应路由器时无法读取未定义的属性“图像” - TypeError: Cannot read property 'image' of undefined while using react router 类型错误:在使用 spotify 的 api 时无法读取未定义的属性 - TypeError: Cannot read property of undefined while using spotify's api TypeError:无法使用React读取undefined的属性 - TypeError: Cannot read property of undefined using React React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom - React, getting Uncaught TypeError: Cannot read properties of null (reading "useRef") while using React Router Dom 反应 useRef 钩子导致“无法读取未定义的属性'0'”错误 - React useRef hook causing “Cannot read property '0' of undefined” error React - 使用 CSS 进行样式设置时,“TypeError:无法读取未定义的属性‘图像’” - React - "TypeError: Cannot read property 'image' of undefined" while styling with CSS 未捕获的类型错误:使用 Rcipe API 时无法读取未定义的属性“映射” - Uncaught TypeError: Cannot read property 'map' of undefined while using Rcipe API TypeError:在使用具有多个输入的表单时无法读取未定义的属性“电子邮件” - TypeError: Cannot read property 'email' of undefined while using form with Multiple Inputs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM