Skip to content

Instantly share code, notes, and snippets.

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 coder054/6d2d3dbb1e8c9711ea821a1c515d43c7 to your computer and use it in GitHub Desktop.
Save coder054/6d2d3dbb1e8c9711ea821a1c515d43c7 to your computer and use it in GitHub Desktop.
//// 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