Consider this Next.js functional component using styled-jsx for CSS-in-JS styling. What will the text color be when rendered?
export default function ColoredText() { return ( <> <p>Hello World</p> <style jsx>{` p { color: blue; } `}</style> </> ) }
Remember that styled-jsx applies scoped styles inside the component.
The styled-jsx block applies the CSS rule p { color: blue; } scoped to this component only. So the paragraph text will be blue.
You want to set the background color dynamically based on a prop bgColor. Which code snippet correctly applies this using styled-jsx?
function Box({ bgColor }) { return ( <> <div className="box">Dynamic Box</div> <style jsx>{` ... `}</style> </> ) }
Use template literals and the variable name directly inside the CSS string.
Option D correctly uses template literals to inject the bgColor variable into the CSS string. Options A and B treat it as a string or invalid CSS. Option D uses props which is undefined in this scope.
In Next.js, when a component using styled-jsx unmounts, what happens to its scoped styles?
Think about how scoped CSS-in-JS libraries manage styles to avoid conflicts.
Styled-jsx automatically removes the scoped styles from the DOM when the component unmounts to prevent style leaks and conflicts.
Look at this component code:
export default function Test() {
return (
<>
Hello
>
)
}The text color stays black. Why?
Styled-jsx scopes styles by rewriting selectors internally.
Styled-jsx scopes styles by adding unique attributes to elements. Using a combined selector like p .text can cause the style not to match because the generated attribute is on the element, so simpler selectors like .text or p work better.
Choose the most accurate advantage of using CSS-in-JS (like styled-jsx) in Next.js over traditional global CSS files.
Think about how CSS-in-JS helps avoid style conflicts in large apps.
CSS-in-JS scopes styles to components, so styles do not leak or override each other unintentionally. This improves maintainability and reduces bugs. The other options are incorrect because CSS-in-JS does not guarantee smaller bundles, does not remove the need to understand CSS, and styles are not always inline but scoped.