简体   繁体   English

JavaScript(React)- 未捕获的类型错误:无法读取未定义的属性(读取“样式”)

[英]JavaScript(React)- Uncaught TypeError: Cannot read properties of undefined (reading 'style')

I am working in a project using React Js.我在一个使用 React Js 的项目中工作。 When I wrote this, Syntax: movieRef.current.style.transform = translateX(210px) ;当我写这篇文章时,语法:movieRef.current.style.transform = translateX(210px) ; it's not working.它不工作。 It shows an error- can't read 'style'.它显示错误 - 无法读取“样式”。 How can I solve this problem.我怎么解决这个问题。 I have attached my codes here.我在这里附上了我的代码。

 import React, { useState, useRef } from "react"; import { useGetMoviesQuery } from "../../services/movieApi"; import SingleMovie from "../SingleMovie/SingleMovie"; import "./MovieRow.scss"; import "../SingleMovie/SingleMovie.scss"; import { MdArrowForwardIos, MdArrowBackIos } from "react-icons/md"; // const MovieRow = ({title}) => {codes} const MovieRow = (props) => { const { title, fetchURI } = props; const { data, isLoading } = useGetMoviesQuery(fetchURI); const movieRef = useRef(); // console.log("data:", data); if (isLoading) { return ( <div> <h1 style={{ textAlign: "center", marginTop: "250px" }}>Loading...</h1> </div> ); } const handleClick = (direction) => { if (direction === "left") { movieRef.current.style.transform = `translateX(210px)`; } }; return ( <> <div className="movie-row-container"> <h1>{title}</h1> <div className="wrapper"> <MdArrowForwardIos className="slider right-arrow" onClick={() => handleClick("right")} /> <div className="movie-row-block" ref={movieRef}> {data?.results.map((movie, index) => ( <SingleMovie key={movie?.id} singleMovie={movie} index={index} /> ))} </div> <MdArrowBackIos className="slider left-arrow" onClick={handleClick("left")} /> </div> </div> </> ); }; export default MovieRow;
 //MovieRow.scss file h1 { font-family: sans-serif; font-size: 100%; color: white; } // non-global css.movie-row-container { margin-top: 25px; margin-left: 22px; .wrapper { position: relative; .slider { height: 100%; width: 70px; background-color: rgba(12, 42, 214, 0.438); color: white; position: absolute; z-index: 99; margin: 0 auto; cursor: pointer; &.right-arrow { right: 0; } &.left-arrow { left: 0; } }.movie-row-block { display: flex; overflow-x: scroll; overflow-y: hidden; transform: translateX(210px); } }.movie-row-block::-webkit-scrollbar { display: none; } }

This part is firing infinitely even before the first render, that's why you'll get undefined on style :这部分甚至在第一次渲染之前就无限触发,这就是为什么你会在style上得到 undefined :

          <MdArrowBackIos
            className="slider left-arrow"
            onClick={handleClick("left")}
          />

Change it to this:将其更改为:

           <MdArrowBackIos
             className="slider left-arrow"
             onClick={handleClick.bind(undefined, "left")}
           />

暂无
暂无

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

相关问题 修复未捕获的类型错误:无法读取未定义的属性(读取“样式”) - Fixing Uncaught TypeError: Cannot read properties of undefined (reading 'style') Router.js:135 Uncaught TypeError: Cannot read properties of undefined (reading &#39;style&#39;) in React Native - Router.js:135 Uncaught TypeError: Cannot read properties of undefined (reading 'style') in React Native 未捕获的类型错误:无法读取未定义的属性(读取“地图”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'map') React 反应:未捕获的类型错误:无法读取未定义的属性(读取“charAt”) - React: Uncaught TypeError: Cannot read properties of undefined (reading 'charAt') 未捕获的 TypeError:无法读取未定义的属性(读取“params”)-React - Uncaught TypeError: Cannot read properties of undefined (reading 'params') - React 未捕获的 TypeError:无法读取 Simple React 项目中未定义(读取“0”)的属性 - Uncaught TypeError: Cannot read properties of undefined (reading '0') in Simple React Project 反应,Redux:未捕获的类型错误:无法读取未定义的属性(读取“区域”) - React, Redux: Uncaught TypeError: Cannot read properties of undefined (reading 'region') 未捕获的类型错误:无法读取未定义的属性(读取“参数”)反应 - Uncaught TypeError: Cannot read properties of undefined (reading 'params') react 未捕获的类型错误:无法读取未定义(读取“过滤器”)JavaScript 的属性 - Uncaught TypeError: Cannot read properties of undefined (reading 'filter') JavaScript JavaScript - 未捕获(承诺)TypeError:无法读取未定义的属性(读取“状态”) - JavaScript - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'status')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM