React import useState - require is not defined

Viewed 55

Attempting to learn React, I have a very simple functional component with a State Hook. It seems like I cannot get the import statement to properly import useState, and I have console error Uncaught ReferenceError: require is not defined. Is there anything else required here to get this component working?

import React, { useState } from 'react';

const LikeButton = () => {
  const [liked, setLiked] = useState(0);

  if (liked) {
    return (
      <i className="fas fa-thumbs-down" onClick={() => setLiked(!liked)}/>
    )
  }
  return (
    <i className="fal fa-thumbs-up" onClick={() => setLiked(!liked)}/>
  )
}

const root = ReactDOM.createRoot(document.querySelector('#like'));
root.render(<LikeButton />);
1 Answers

The answer is to use React.useState(0) instead of the import statement.

Related