Created
November 1, 2018 01:04
-
-
Save theJasonJones/cd479c2cd7dc7612c1fd93762f608d0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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