Skip to content

Instantly share code, notes, and snippets.

@Jaikant
Created August 10, 2018 01:52
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 Jaikant/b13fac1ed431b5d14f0738aa79b337df to your computer and use it in GitHub Desktop.
Save Jaikant/b13fac1ed431b5d14f0738aa79b337df to your computer and use it in GitHub Desktop.
Function components to class components
<!DOCTYPE html>
<html>
<head>
<title>First React App</title>
<script crossorigin src="https://unpkg.com/react@16/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script>
<script src='https://unpkg.com/babel-standalone@6/babel.min.js'></script>
</head>
<body>
<div id='app'></div>
<script type='text/babel'>
let friends = ['Max', 'Sid', 'Aman']
function FriendList(props) {
return (
<ul>
{props.list.map((friend) => (
<li key={friend}>
{friend}
<button onClick={() => props.onRemoveFriend(friend)}> remove </button>
</li>
))}
</ul>
)
}
function handleRemoveFriend(name) {
console.log("Removing friend: ", name)
friends = friends.filter((friend) => friend != name)
console.log("The friends are: ", friends)
}
function App() {
return (
<div>
Hello friends!
<FriendList list={friends}
onRemoveFriend={handleRemoveFriend} />
</div>
)
}
ReactDOM.render(
<App/>,
document.getElementById('app')
)
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment