Skip to content

Commit

Permalink
测试自定义NavLink样式
Browse files Browse the repository at this point in the history
  • Loading branch information
zoujia committed Jun 3, 2024
1 parent 37cad4a commit a8afa9d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
3 changes: 2 additions & 1 deletion src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,8 @@ i {
background: #e3e3e3;
}

#sidebar nav a.active {
#sidebar nav a.active,
#sidebar nav a.active2 {
background: hsl(224, 98%, 58%);
color: white;
}
Expand Down
6 changes: 5 additions & 1 deletion src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import DestroyContact, {
} from './routes/destroy';
import Index from './routes';

function TestSpinner() {
return <div>Fallback-Spinner...</div>;
}

// Solution-2: create routes using JSX
const routerUsingElements = createBrowserRouter(
createRoutesFromElements(
Expand Down Expand Up @@ -96,6 +100,6 @@ const router = createBrowserRouter([

ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<RouterProvider router={routerUsingElements} />
<RouterProvider router={routerUsingElements} fallbackElement={<TestSpinner />} />
</React.StrictMode>,
);
5 changes: 4 additions & 1 deletion src/routes/root.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,11 @@ export default function Root() {
<li key={contact.id}>
<NavLink
to={`contacts/${contact.id}`}
// passing a function to className
// to customzie the state class
// 'active' is default for active-state
className={({isActive, isPending}) => {
return isActive ? 'active' : (isPending ? 'pending' : '');
return isActive ? 'active2' : (isPending ? 'pending' : '');
}}
>
{contact.first || contact.last ? (
Expand Down

0 comments on commit a8afa9d

Please sign in to comment.