Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created March 16, 2023 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/c1e5f78aa7bbe0eec470b6e002736471 to your computer and use it in GitHub Desktop.
Save codecademydev/c1e5f78aa7bbe0eec470b6e002736471 to your computer and use it in GitHub Desktop.
Codecademy export
import React {useState} from 'react';
import { generateId, getNewExpirationTime } from './utilities';
export function AddThoughtForm(props) {
const [text, setText] = useState('');
const handleTextChange = (e) => {
setText(e.target.value)
}
const handleSubmit = (e) => {
e.preventDefault();
}
return (
<form className="AddThoughtForm" onSubmit={handleSubmit}>
<input
value={text}
onChange={handleTextChange}
type="text"
aria-label="What's on your mind?"
placeholder="What's on your mind?"
/>
<input type="submit" value="Add" />
</form>
);
}
import React, { useState } from 'react';
import ReactDOM from 'react-dom';
import { AddThoughtForm } from './AddThoughtForm';
import { Thought } from './Thought';
import { generateId, getNewExpirationTime } from './utilities';
function App() {
const [thoughts, setThoughts] = useState([
{
id: generateId(),
text: 'This is a place for your passing thoughts.',
expiresAt: getNewExpirationTime(),
},
{
id: generateId(),
text: "They'll be removed after 15 seconds.",
expiresAt: getNewExpirationTime(),
},
]);
const addThought = (thought) => {
setThoughts((prev) => [...prev, thought]);
}
return (
<div className="App">
<header>
<h1>Passing Thoughts</h1>
</header>
<main>
<AddThoughtForm addThought={addThought} />
<ul className="thoughts">
{thoughts.map((thought) => (
<Thought key={thought.id} thought={thought} />
))}
</ul>
</main>
</div>
);
}
ReactDOM.render(<App />, document.getElementById('app'));
import React from 'react';
export function Thought(props) {
const { thought, removeThought } = props;
const handleRemoveClick = () => {
removeThought(thought.id);
};
return (
<li className="Thought">
<button
aria-label="Remove thought"
className="remove-button"
onClick={handleRemoveClick}
>
&times;
</button>
<div className="text">{thought.text}</div>
</li>
);
}
export function getNewExpirationTime() {
return Date.now() + 15 * 1000;
}
let nextId = 0;
export function generateId() {
const result = nextId;
nextId += 1;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment