Created
August 4, 2022 04:31
-
-
Save coder054/6d2d3dbb1e8c9711ea821a1c515d43c7 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
//// Demo of render props pattern in React | |
//// Provider pattern in Emberjs (https://guides.emberjs.com/release/tutorial/part-2/provider-components/) | |
import React from 'react'; | |
import './style.css'; | |
const RentalsFilter = ({ rentals, query, render }) => { | |
const filteredRentals = React.useMemo(() => { | |
if (query) { | |
rentals = rentals.filter((rental) => rental.title.includes(query)); | |
} | |
return rentals; | |
}, [rentals, query]); | |
return render(filteredRentals); | |
}; | |
export default function App() { | |
const [query, setQuery] = React.useState(''); | |
return ( | |
<div> | |
<input | |
value={query} | |
onChange={(e) => { | |
setQuery(e.target.value); | |
}} | |
/> | |
<RentalsFilter | |
query={query} | |
rentals={[ | |
{ title: 'Grand Old Mansion' }, | |
{ title: 'Urban Living' }, | |
{ title: 'Downtown Charm' }, | |
]} | |
render={(filteredRentals) => { | |
return filteredRentals.map((o) => <li>{o.title}</li>); | |
}} | |
/> | |
</div> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment