import React, { useState, useRef } from "react";
import { Link, animateScroll as scroll } from "react-scroll";
function Acc(props) {
const content = useRef(null);
const [setActive, setActiveState] = useState("");
const [setScroll, setScrollState] = useState();
function toggle() {
setActiveState(setActive === "" ? "active" : "");
setScrollState(setScroll === "active" ? <Link to={id} smooth={true} duration={500} spy={true} exact={true}></Link> : );
}
return (
<div className="a1" id={props.id}>
<div className="a2">
<div className="a3">
<button className="button" onClick={toggle}>
//image goes here
</button>
</div>
</div>
</div>
);
}
export default Acc;
Why am I getting this error?
./src/components/Acc.js
SyntaxError: /Users/me/Desktop/School/aapp/src/components/Acc.js: Unexpected token (24:121)
23 | setRotateState(setActive === "active" ? "icon" : "icon rotate");
> 24 | setScrollState(setScroll === "active" ? <Link to={id} smooth={true} duration={500} spy={true} exact={true}></Link> : );
| ^
25 | }
26 |
27 | return (
How do I fix it? The aim is to create smooth scroll action to the necessary div (classname='a1' which has an id
) when the button is clicked. The button must smooth scroll as well as trigger toggle()
.
Any help would be greatly appreciated !
The error as Ken White pointed out is because of this:
Scroll to the end of the line for the problem.
If you're using a ternary statement you need to have something on the left and the right side of the colon
:
.So something like this would be correct:
Doesn't have to be
null
, but you need to put something there.Now I'm not entirely sure what you're trying to do, but from your code it seems you want to conditionally render a
Link
on button press. And when you press on it, the page should smooth scroll to anElement
with a particularname
.Here's a simple example combining conditional rendering and smooth scrolling with
react-scroll
:I've replaced your
setActive
andsetScroll
states with a singleisActive
state. At least for conditionally rendering theLink
you don't need more than one piece of state.sandbox example
Update
If you want the button to scroll there is no need for the
Link
and you could just do something like this:sandbox example