I have a simple code snippet that runs fine. I need to add a call to 'useState()'. I tried using an import and using 'react.useState()', neither of which work. Is it even possible?
1 Answer
Your description of the problem is not very complete. What code are you actually trying in the Snippet? Do you get an error message appearing in the console or elsewhere? What actually happens? Just like on the main site, it's more efficient to spell out the precise problem and behavior, rather than making us guess.
It seems to me that these answers use this feature for demonstration purposes. You can probably follow their example to get it to work.
It looks like useState
is only supported with ReactJS 16.7 (or maybe 16.8?) and later. The latest version that Stack Snippets supports for automatic importing in the Options pane is 16.6.3. Therefore, I'd suggest that you import the library manually from a CDN to ensure that you're getting a new enough version with support for useState
.