liveBook · Manning

Prop

As explained in D3.js in Action, Third Edition

React props are read-only input values passed from a parent component to a child component to configure rendering or behavior.

A prop is an input passed from a parent React component to a child. A functional component receives props as a JavaScript argument and uses them to decide what JSX to return. For example, after loading dashboard data, App passes it to Charts with <Charts data={data} />. section 8.3

Props make chart components reusable. A scatterplot receives data, margins, and a color scale; it passes dimensions and margins to ChartContainer. For each data item, it calculates a position with D3 scales and passes the resulting coordinates, radius, and color to a Circle component. React renders the circles rather than asking D3 to add them to the DOM. section 8.6.1

Configure one circle

A parent can render <Circle cx={xScale(item.user_count)} cy={yScale(item.retention_percentage)} r={6} fill={colorScale(item.id)} />. Circle uses props.cx, props.cy, props.r, and props.fill as SVG attributes. It also accepts optional stroke and strokeWidth props, supplying defaults when they are absent. section 8.6.1

Listing 8.9 Preparing the Circle component (Circle.js)
const Circle = props => {
  return (
    <circle
      cx={props.cx}
      cy={props.cy}
      r={props.r}
      fill={props.fill}
      stroke={props.stroke ? props.stroke : "none"}
      strokeWidth={props.strokeWidth ? props.strokeWidth : 0}
    />
  )
};

export default Circle;

See how the small Circle component turns received props into a reusable SVG element. section 8.6.1

Props can carry behavior as well as values. A rankings component passes an onFilterSelection callback to its filter controls. Clicking a button calls that callback with the selected ID; the parent updates its active-filter state, and the visualization changes. In a hybrid React–D3 curve, incoming data props also drive an effect that transitions the SVG path. section 8.7 section 8.7

How D3.js in Action, Third Edition covers it

How deep it goes
Works through prop handoffs in chart components and filter controls, including optional attributes and callback-driven state changes.
Examples
Uses loaded dashboard data, scatterplot circles and axes, filter buttons, and animated ranking curves.
What it assumes
Introduces functional components as JavaScript functions returning JSX before using props in D3-powered charts.
What you can do after
Apply it via React to configure reusable chart components and pass filter callbacks that update a visualization.
How it connects
Connects JSX, functional components, SVG attributes, D3 scales, React state, and effects.

Also explained in

Compare how 5 books explain prop →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of D3.js in Action, Third Edition. The section links point to the parts of the book it draws on.