Skip to content

Instantly share code, notes, and snippets.

@dmiller9911
Created January 30, 2017 02:53
Show Gist options
  • Save dmiller9911/51afbe64e7bb20b6a36db1133bc7ed00 to your computer and use it in GitHub Desktop.
Save dmiller9911/51afbe64e7bb20b6a36db1133bc7ed00 to your computer and use it in GitHub Desktop.
React.Children.map example
import React from 'react';
import * as ReactDOM from 'react';
let notifications = [];
function addNotification(notification) {
notifications = [...notifications, notification];
}
function removeNotification(notification) {
const index = notifications.indexOf(notification);
notifications = [
...notifications.splice(0, index),
...notifications.splice(index + 1)
];
}
function Application({ children }) {
const extendedChildren = React.children.map(children, (child) => {
return React.cloneElement(
child,
{
notifications,
removeNotification,
addNotification
}
);
});
return (
<div>
{extendedChildren} // Children now have their existing props and the notification callbacks and list.
</div>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment