features of component containment
all components have an implicit children prop by default
what is a react element
example of component containment + specialization
function ConfirmationDialog() {
return (
<Dialog color="blue">
<h1 className="Dialog-title">
Thanks!
</h1>
<p className="Dialog-message">
We’ll process your order in less than 24 hours.
</p>
</Dialog>
);
}
React.cloneElement API use cases
Incorrect use of React.Children.map
const Row = ({ children, spacing }) => {
const childStyle = {
marginLeft: `${spacing}px`,
};
return(
<div className="Row">
// mutates each child
{React.Children.map(children, (child, index) => {
child.props.style = {
...child.props.style,
...(index > 0 ? childStyle : {}),
};
return child;
})}
// You should use React.cloneElement to create a copy first
{React.Children.map(children, (child, index) => {
return React.cloneElement(child, {
child.props.style = {
...child.props.style,
...(index > 0 ? childStyle : {}),
},
});
})}
</div>
);
};
what would be logged into the console when clicking the Submit button that gets rendered on the screen?
const Button = ({ children, ...rest }) => (
<button onClick={() => console.log("ButtonClick")} {...rest}>
{children}
</button>
);
const withClick = (Component) => {
const handleClick = () => {
console.log("WithClick");
};
return (props) => {
return <Component onClick={handleClick} {...props} />;
};
};
const MyButton = withClick(Button);
export default function App() {
return <MyButton onClick={() => console.log("AppClick")}>Submit</MyButton>;
}
due to the order of the spread operator in the different components,
the original onClick prop passed to MyButton takes precedence
output: "AppClick"
valid solutions to encapsulate cross-cutting concerns
What does the screen utility object from react-testing-library represent when performing queries against it?
When writing tests with Jest and react-testing-library, what matcher would you have to use to assert that a button is disabled?