react create a page with a button that increments a number, which is shown as a text
- User
react create a page with a button that increments a number, which is shown as a text
Assistant
Sure! Here is an example of how you could create a React component that displays a number and increments it every time the button is clicked:
import React, { useState } from 'react'; function Counter() { // Declare a state variable called "count" and initialize it to 0 const [count, setCount] = useState(0); // Define a function called "increment" that increments the "count" state variable by 1 const increment = () => { setCount(count + 1); }; // Render the button and the current count return ( <div> <button onClick={increment}>Increment</button> <p>Count: {count}</p> </div> ); } export default Counter;To use this component in your application, you would import it and render it like this:
import React from 'react'; import Counter from './Counter'; function App() { return ( <div> <Counter /> </div> ); } export default App;
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.