Basic reactjs app
- User
Basic reactjs app
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Basic reactjs app
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'll help you create a basic React.js app. Here's a simple example with a counter application:
Create an index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Basic React App</title>
<script crossorigin src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const { useState } = React;
function App() {
const [count, setCount] = useState(0);
const [name, setName] = useState('');
return (
<div style={{ padding: '20px', fontFamily: 'Arial, sans-serif' }}>
<h1>Basic React App</h1>
<div style={{ marginBottom: '20px' }}>
<h2>Counter: {count}</h2>
<button onClick={() => setCount(count + 1)}>Increment</button>
<button onClick={() => setCount(count - 1)}>Decrement</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
<div>
<h2>Hello, {name || 'Guest'}!</h2>
<input
type="text"
placeholder="Enter your name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
</div>
);
}
ReactDOM.createRoot(document.getElementById('root')).render(<App />);
</script>
</body>
</html>
If you have Node.js installed, you can create a more structured React app:
npx create-react-app my-app
cd my-app
npm start
Then replace the contents of src/App.js:
import React, { useState } from 'react';
import './App.css';
function App() {
const [count, setCount] = useState(0);
const [name, setName] = useState('');
const [todos, setTodos] = useState([]);
const [inputValue, setInputValue] = useState('');
const addTodo = () => {
if (inputValue.trim()) {
setTodos([...todos, inputValue]);
setInputValue('');
}
};
const removeTodo = (index) => {
setTodos(todos.filter((_, i) => i !== index));
};
return (
<div className="App">
<header className="App-header">
<h1>Basic React App</h1>
{/* Counter Section */}
<div className="section">
<h2>Counter: {count}</h2>
<div className="button-group">
<button onClick={() => setCount(count + 1)}>+</button>
<button onClick={() => setCount(count - 1)}>-</button>
<button onClick={() => setCount(0)}>Reset</button>
</div>
</div>
{/* Greeting Section */}
<div className="section">
<h2>Hello, {name || 'Guest'}!</h2>
<input
type="text"
placeholder="Enter your name"
value={name}
onChange={(e) => setName(e.target.value)}
/>
</div>
{/* Todo List Section */}
<div className="section">
<h2>Todo List</h2>
<div className="todo-input">
<input
type="text"
placeholder="Add a todo"
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={(e) => e.key === 'Enter' && addTodo()}
/>
<button onClick={addTodo}>Add</button>
</div>
<ul className="todo-list">
{todos.map((todo, index) => (
<li key={index}>
{todo}
<button onClick={() => removeTodo(index)}>Remove</button>
</li>
))}
</ul>
</div>
</header>
</div>
);
}
export default App;
And update src/App.css:
.App {
text-align: center;
}
.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}
.section {
margin: 20px 0;
padding: 20px;
border: 1px solid #61dafb;
border-radius: 8px;
min-width: 300px;
}
.button-group {
display: flex;
gap: 10px;
justify-content: center;
margin-top: 10px;
}
button {
background-color: #61dafb;
border: none;
color: #282c34;
padding: 10px 20px;
font-size: 16px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
button:hover {
background-color: #4fa8c5;
}
input {
padding: 10px;
font-size: 16px;
border-radius: 4px;
border: 1px solid #ccc;
margin-right: 10px;
}
.todo-input {
display: flex;
justify-content: center;
margin-bottom: 20px;
}
.todo-list {
list-style: none;
padding: 0;
}
.todo-list li {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
margin: 5px 0;
background-color: rgba(255, 255, 255, 0.1);
border-radius: 4px;
}
This basic React app demonstrates:
useStateTo run Option 1, just open the HTML file in a browser. For Option 2, you'll need Node.js installed and can run npm start after creating the app.