You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nesting is useful when instances of one component will contain instances of other components. Nesting can be achieved by invoking child components within the update function of the parent component, possibly deriving the value of children props from the props passed into the parent component.
Here's an example of a post component that composes two other components, heading and paragraph.
Sometimes children components are not known in advance. This is often the case for components that serve as "boxes" that can contain arbitrary content, such as cards or dialogs. There are no special constructs provided for achieving this, but it can be achieved by using a pattern in which the child component and its props are both passed in via the props of the parent component.
Here's an example of a card component that can render arbitrary children.
Sometimes components should render sub-components only under certain conditions. To achieve this, the props passed into the sub-component can either be [] to render zero component instances, or any other value to render one or many component instances. Even if a sub-component is not rendered, it still needs to be invoked with its props as [], in the case that it was rendered previously and its instances need to be removed from the DOM.
Here's an example of a fruit component that conditionally renders either apple or orange components.
The goal is to enable developers to create and use "components" (similar to stateless functional components in React) without leaving the realm of D3. I felt the need to create this when I found myself reaching for React just to add dynamic user interface elements to D3 visualizations, or to embed visualizations inside a complex/dynamic UI. I felt these things should be easy to do within D3, without adding the React+JSX+Babel+Webpack toolchain into the mix, and without implementing the Towards Reusable Charts boilerplate for each and every level of my DOM tree.
So, this is my attempt at an abstraction that may help bridge the feature gap between React and D3. I'd like to share it here and get feedback on the concept / implementation. Any thoughts? Is it potentially useful? Potentially an addition to D3 proper? A silly endeavor given the popularity of React to achieve the same thing? Any feedback is warmly welcome, and feel free to create a new issue for discussions / feature requests / requests for examples.
The following feels like it does not belong in the README. Maybe a Medium post would be better? Or in a separate .md file? Stashing it here for now.
Composing Components
Components can use other components in their update functions. Some useful patterns for component composition are:
Nesting
Nesting is useful when instances of one component will contain instances of other components. Nesting can be achieved by invoking child components within the update function of the parent component, possibly deriving the value of children
props
from theprops
passed into the parent component.Here's an example of a
post
component that composes two other components,heading
andparagraph
.Here's how we would render an instance of the
post
component.The following DOM structure will be rendered.
Here's an example of rendering multiple component instances.
The following HTML structure will be rendered.
For a full working example using the components above, see Posts with d3-component.
Containment
Sometimes children components are not known in advance. This is often the case for components that serve as "boxes" that can contain arbitrary content, such as cards or dialogs. There are no special constructs provided for achieving this, but it can be achieved by using a pattern in which the child component and its props are both passed in via the props of the parent component.
Here's an example of a
card
component that can render arbitrary children.Here's how we can use this
card
component to render a card that contains instances of thepost
component.The following DOM structure will be rendered.
Conditional Rendering
Sometimes components should render sub-components only under certain conditions. To achieve this, the
props
passed into the sub-component can either be[]
to render zero component instances, or any other value to render one or many component instances. Even if a sub-component is not rendered, it still needs to be invoked with itsprops
as[]
, in the case that it was rendered previously and its instances need to be removed from the DOM.Here's an example of a
fruit
component that conditionally renders eitherapple
ororange
components.Here's how we can use this
fruit
component.The following DOM structure will be rendered.
The text was updated successfully, but these errors were encountered: