Created
May 2, 2020 06:30
-
-
Save sagar-gavhane/3579c4e64227cdddd40193de41e656ae to your computer and use it in GitHub Desktop.
#2 Common fixing scenarios for object & function props 2
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 { render } from 'react-dom' | |
function Alert(props) { | |
return ( | |
<div | |
className="alert-wrapper" | |
style={{ display: props.showAlert ? 'block' : 'none' }} | |
> | |
<div className="alert-close" onClick={props.handleCloseAlert}> | |
X | |
</div> | |
<div className="alert-title">{props.alertData.title}</div> | |
<div className="alert-description">{props.alertData.description}</div> | |
</div> | |
) | |
} | |
function App() { | |
const [showAlert, setShowAlert] = React.useState(false) | |
const [counter, setCounter] = React.useState(0) | |
const alertData = React.useMemo( | |
() => ({ | |
title: 'There was an error processing your request', | |
description: 'Please try again...', | |
}), | |
[] | |
) | |
const handleShowAlert = React.useCallback(() => { | |
setShowAlert(true) | |
}, []) | |
const handleCloseAlert = React.useCallback(() => { | |
setShowAlert(false) | |
}, []) | |
const handleIncrementCounter = React.useCallback(() => { | |
setCounter(counter + 1) | |
}, [counter]) | |
return ( | |
<div> | |
<button onClick={handleIncrementCounter}>counter: {counter}</button> | |
<button onClick={handleShowAlert}>Show Me Alert</button> | |
<Alert | |
showAlert={showAlert} | |
alertData={alertData} | |
handleCloseAlert={handleCloseAlert} | |
/> | |
</div> | |
) | |
} | |
render(<App />, document.getElementById('root')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment