Created
January 30, 2017 02:53
-
-
Save dmiller9911/51afbe64e7bb20b6a36db1133bc7ed00 to your computer and use it in GitHub Desktop.
React.Children.map example
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 * 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