Nice, I love it, thanks for the great article!

I see useState written a lot a little differently, without {}, are both correct?

My code example:

const [isDragging, setDragging] = useState(false);/* We'll use a `ref` to access the DOM element that the `motion.li` produces. This will allow us to measure its height and position, which will be useful to decide when a dragging element should switch places with its siblings. */const ref = useRef(null);/* By manually creating a reference to `dragOriginY` we can manipulate this value if the user is dragging this DOM element while the drag gesture is active to compensate for any movement as the words are re-positioned. */const dragOriginY = useMotionValue(0);/* Update the measured position of the word so we can calculate when we should rearrange. */useEffect(() => { setPosition(i, { height: ref.current.offsetHeight, top: ref.current.offsetTop }); });

vs. your code example:

import React, { useState } from 'react';

function Example() {
const [state, setState] = useState({counter:0});
const add1ToCounter = () => {
const newCounterValue = state.counter + 1;
setState({ counter: newCounterValue});
}

return (
<div>
<p>You clicked {state.counter} times</p>
<button onClick={add1ToCounter}>
Click me
</button>
</div>
);
}

My question is… How would you just toggle true / false?

import React, { useState } from 'react';

function Example() {
const [state, setState] = useState({false});
const toggleCounter = () => {
const newCounterValue = !state.counter;
setState({ counter: newCounterValue});
}

return (
<div>
<p>You clicked {state.counter} times</p>
<button onClick={toggleCounter}>
Click me
</button>
</div>
);
}

Written by

πŸ€“ The physical therapist who writes JavaScript πŸ’ͺ Web Developer 😎 Mentor 🧠 DPT πŸ˜„ SEO Expert 😁 React πŸ˜† Jamstack πŸ—―οΈ Ask me anything πŸ‘‰ DoctorDerek.com πŸ‘ˆ

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store