Created
June 5, 2019 14:17
-
-
Save oncomouse/bdf0131918ac1d1f03e5230564f4d063 to your computer and use it in GitHub Desktop.
DHSI 2019 - Day 2 Exercise Solutions
This file contains hidden or 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 { longestWord } from './day01/exercises'; | |
const LongestWord = () => { | |
const [words, setWords] = useState(''); | |
const onChange = (event) => setWords(event.target.value); | |
return ( | |
<div> | |
<h1>Your Longest Word: {longestWord(words)}</h1> | |
<textarea | |
rows="24" | |
cols="80" | |
onChange={onChange} | |
value={words} | |
/> | |
</div> | |
); | |
} | |
export default LongestWord; |
This file contains hidden or 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 from 'react'; | |
import { remove } from 'ramda'; | |
/* | |
This is the Names.js file we wrote as a class but with the addition of user deletion. | |
I've marked the changes below. | |
*/ | |
const Names = (props) => { | |
const { | |
names, | |
setNames // Now we also need setNames (to delete from an array) | |
} = props; | |
/* | |
This function is new. It is passed an array index. When run, it asks the user to confirm deletion and if confirmed, it will delete the item at the supplied index. | |
If you have questions about the remove call, see the Ramda documentation: https://ramdajs.com/docs/#remove | |
*/ | |
const removeName = (index) => { | |
const yesDelete = global.confirm(`Are you sure you want to delete ${names[index]}?`); | |
if(yesDelete) { | |
setNames(remove(index, 1)); | |
} | |
} | |
return (<ul> | |
{names.map((name, index) => ( | |
<li | |
key={index} | |
> | |
{name} | |
<small> | |
<button | |
onClick={() => removeName(index)} | |
>x</button> | |
{/* We attach an anonymous funciton as an event handler because we don't need event.target to remove our array, but we do need the array index. This is perfectly acceptable React code. */} | |
</small> | |
</li> | |
))} | |
</ul>) | |
} | |
export default Names; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment