Skip to content

Instantly share code, notes, and snippets.

@theJasonJones
Created November 1, 2018 01:04
Show Gist options
  • Save theJasonJones/cd479c2cd7dc7612c1fd93762f608d0d to your computer and use it in GitHub Desktop.
Save theJasonJones/cd479c2cd7dc7612c1fd93762f608d0d to your computer and use it in GitHub Desktop.
import React, { useState } from "react";
import "./App.css";
import Form from "./Form";
const App = () => {
const [todos, setTodos] = useState([]);
const toggleComplete = i =>
setTodos(todos.map(
(todo, k) =>
k === i
? {
...todo,
complete: !todo.complete,
}
: todo
));
return (
<div className="App">
<Form
onSubmit={inputText =>
setTodos([{ inputText, complete: false }, ...todos])
}
/>
<div>
{todos &&
todos.map(({ inputText, complete}, i) => (
<div
key={inputText}
onClick={() => toggleComplete(i)}
style={{
textDecoration: complete ? "line-through" : "",
color: complete ? "red" : "black",
}}>
{inputText}
</div>
))}
</div>
</div>
);
};
export default App;
import React, { useState } from "react";
import "./App.css";
const useInputValue = initialValue => {
const [value, setValue] = useState(initialValue);
return {
value,
onChange: e => setValue(e.target.value),
resetValue: () => setValue(""),
};
};
const Form = ({ onSubmit }) => {
const {resetValue, ...inputText} = useInputValue("");
return (
<form
onSubmit={e => {
e.preventDefault();
onSubmit(inputText.value);
resetValue()
}}
>
<input {...inputText} />
</form>
);
};
export default Form;
{
"name": "todo",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.7.0-alpha.0",
"react-dom": "^16.7.0-alpha.0",
"react-scripts": "2.0.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment