简体   繁体   English

对象可能是“未定义” ts2532

[英]object is possibly "undefined" ts2532

I get the error when I want to make a slider and I have looked for several alternatives to solve it but none of them work.当我想制作一个滑块时出现错误,我已经寻找了几种解决方法,但它们都不起作用。 The error is in useEffect(() => {setWidth ( carousel.current.scrollWidth - carousel.current.offsetWidth)}, []);错误在于 useEffect(() => {setWidth ( carousel.current.scrollWidth - carousel.current.offsetWidth)}, []); and in carousel.current并在 carousel.current

 import React from "react"; import { motion } from "framer-motion"; import { useRef, useEffect, useState } from "react"; import images from "../images"; function Sliders() { const [width, setWidth] = useState(0); const carousel = useRef(); useEffect(() => { setWidth (carousel.current.scrollWidth - carousel.current.offsetWidth) }, []); return ( <div className="Sliders"> <motion.div red={carousel} className="carusel" whileTap={{cursor: "grabbing"}} > <motion.div drag="x" dragConstraints={{ right: 0, left: -width}} className="inner-carousel" > {images.map((image) => { return ( <motion.div className="item" key={image}> <img src={image} alt="" /> </motion.div> ); })} </motion.div> </motion.div> </div> ); } export default Sliders

When the component is launched, initially the value for carousel.current is undefined.启动组件时,最初carousel.current的值是未定义的。 Just wrap the statement in if condition, and I believe it should work.只需将语句包装在 if 条件中,我相信它应该可以工作。

useEffect(() => {
  if (carousel?.current) {
    setWidth (carousel.current.scrollWidth -  carousel.current.offsetWidth)
  }
}, []);

Note: You can try to console carousel.current before return statement to be sure when the object is undefined and at what point it gets populated.注意:您可以尝试在 return 语句之前控制 carousel.current 以确定对象何时未定义以及何时填充它。

暂无
暂无

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

相关问题 Ts2532、Object 可能是“未定义” - Ts2532, Object is possibly 'undefined' TS2532:对象可能是“未定义”。 在 array.map() 上 - TS2532: Object is possibly 'undefined'. on array.map() 错误 TS2532:对象可能是“未定义” - 使用“?” 操作员 - error TS2532: Object is possibly 'undefined' - Using "?." operator TypeScript 2:Import语句生成TS2532:“对象可能是&#39;undefined&#39;。” - TypeScript 2: Import statement generates TS2532: “Object is possibly 'undefined'.” 打字稿数组语义错误 TS2532:对象可能是“未定义” - Typescript array semantic error TS2532: Object is possibly 'undefined' TypeScript 错误 TS2532:Object 可能是“未定义”? - TypeScript Error TS2532: Object is possibly 'undefined'? Typescript 显示“this”错误 Object 说 TS2532: Object 在 vue 方法中可能是“未定义” - Typescript displays error for "this" Object saying TS2532: Object is possibly 'undefined' inside of vue methods 类型保护错误TS2532后,可能未定义Typescript对象 - Typescript Object is possibly undefined after type guard error TS2532 在另一个函数中使用时,变量可能未定义(打字稿“错误TS2532”) - Variable possibly undefined when used inside another function (Typescript “error TS2532) 打字稿-内联未定义检查不起作用(对象可能是&#39;undefined&#39;.ts(2532)) - Typescript - Inline undefined check not working (Object is possibly 'undefined'.ts(2532))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM