0

This line of codes get error

listRef.current.style.transform = 'translateX(${230 + distance}px)'

    import { ArrowBackIosOutlined, ArrowForwardIosOutlined } from '@material-ui/icons';
    import React, { useRef } from 'react'
    import "./list.scss";
    import ListItem from "../listItem/ListItem";
    
    export default function List() {
    
      const listRef = useRef()
    
      const handleClick = (direction) => {
        let distance = listRef.current.getBoundingClientRect().x - 50
        if(direction === "left"){
          listRef.current.style.transform = 'translateX(${230 + distance}px)'
        }
      }
    
      return (
        <div className='list'>
            <span className="listTitle">Continue to watch</span>
            <div className="wrapper">
                <ArrowBackIosOutlined className="sliderArrow left" onClick={()=>handleClick("left")}/>
                <div className="container" ref={listRef}>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                    <ListItem/>
                </div>
                <ArrowForwardIosOutlined className="sliderArrow right" onClick={()=>handleClick("right")}/>
            </div>
        </div>
      )
    }

1
  • ${xxx} works inside `` only ... not '' or "" Commented Mar 19, 2022 at 7:15

1 Answer 1

1

Change you code from

 const handleClick = (direction) => {
    let distance = listRef.current.getBoundingClientRect().x - 50
    if(direction === "left"){
      listRef.current.style.transform = 'translateX(${230 + distance}px)'
    }
  }

to

 const handleClick = (direction) => {
    let distance = listRef.current.getBoundingClientRect().x - 50
    if(direction === "left"){
      listRef.current.style.transform = `translateX(${230 + distance}px)`
    }
  }

What you need is called template literal in js.

Type Template Literal:

Mac: Shift + ` on keyboard

Refer to: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.