Question · JavaScript and React course · Module 6: React Router
Dynamic CSS class in NavLink
How does NavLink in React Router allow you to dynamically set a CSS class based on the active state?
Answer options
- NavLink automatically adds the 'active' class without any configuration
- You need to manually check the URL in useEffect and add the class via DOM API
- Through the className prop accepting a function with { isActive } parameter, e.g., className={({ isActive }) => isActive ? 'active' : ''}
- NavLink does not support dynamic classes - you need to use the Link component with additional logic
You can check the correct answer by solving this task in the CodeWorlds game.