Skip to content

Instantly share code, notes, and snippets.

@Magellol
Last active September 7, 2017 18:52
Show Gist options
  • Save Magellol/81ab9765fe7af4ad24969b7728bbb5b1 to your computer and use it in GitHub Desktop.
Save Magellol/81ab9765fe7af4ad24969b7728bbb5b1 to your computer and use it in GitHub Desktop.
Give the developer the ability to have more flexibility on the layout if a function is passed as children.
const propTypes = {
children: PropTypes.oneOfType([PropTypes.func, PropTypes.node]),
};
const defaultProps = {
children: null,
};
const SaveButton = () => ...
const ActionsContainer = ({ children }) => ...
const ConfigurationPanel = ({ children, onSave }) =>
<div>
{typeof children === 'function'
? children({
ActionsContainer,
SaveButton: () => <SaveButton handleClick={onSave} />,
})
: <div>
{children}
<ActionsContainer>
<SaveButton handleClick={onSave} />
</ActionsContainer>
</div>}
</div>;
// Simple usage, less flexiblity on the rendered layout.
const App = () => (
<ConfigurationPanel>
<div>Some children as node here</div>
</ConfigurationPanel>
);
// Advanced usage, more flexiblity on the rendered layout.
const App = () => (
<ConfigurationPanel>
{({ ActionsContainer, SaveButton }) => (
<div>Some children></div>
<ActionsContainer>
<CancelButton />
<SaveButton />
</ActionsContainer>
)}
</ConfigurationPanel>
);
ReactDOM.render(<App />, document.getElementById('root'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment