React-router将索引路由设置为活动v6。

huangapple go评论65阅读模式
英文:

React-router set index route as active v6

问题

I can help you translate the provided content. Here's the translation:

我正在尝试创建一个具有子路由的路由。
当我导航到“/”(没有路由)时,它应该定位到“/weather”(又名首页),这通过Navigate工作正常。

然后,“index”子路由显示正确。但是,在Weather.tsx中,Navlink没有获得“active”状态。
当我点击“weather-forecast”链接时,它会突出显示,因为路由匹配。

但是,当“index”是所选路由时,我该如何使选项卡处于活动状态?

或者也许我在路由设置方面做错了什么?

Please note that I've translated the content as requested, focusing on the code-related parts. If you need further assistance or have any questions about the code, feel free to ask.

英文:

I am trying to create a route with child routes.
When I navigate to "/" (no route) it should land on /weather (aka Home) and that works with Navigate.

Then the index child route shows correctly. But the Navlink do not get "active" in Weather.tsx.
And when I click "weather-forecast"-link it gets higlighted obviosly because the route then match.

But how do I have the tab active when index is the selected route?

Or maybe I have done something wrong with the setup of the routes?

App.tsx

const Header = styled.div`
	grid-area: header;
	nav {
		height: 100%;
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	h1 {
		font-size: clamp(1rem, 4vw, 3rem);
		margin: 0;
		margin-left: 15px;
		color: #921267;
	}

	ul {
		list-style-type: none;
		display: flex;
		margin-right: 15px;
	}

	a {
		all: unset;
		cursor: pointer;
		&:hover {
			color: #205ad2;
		}
		&.active {
			color: #205ad2;
		}
	}

	ul li:last-child {
		margin-left: 10px;
	}
`;
    <Header>
    				<nav>
    					<h1>Weather App</h1>
    					<ul>
    						<li>
    							<NavLink to="weather">Weather</NavLink>
    						</li>
    						<li>
    							<NavLink to="about">About</NavLink>
    						</li>
    					</ul>
    				</nav>
    			</Header>
    			<MainContent>
    				<Suspense fallback="Loading...">
    					<Routes>
    						<Route path="" element={<Navigate to="weather" />} />
    						<Route path="weather" element={<Weather />}>
    							<Route index element={<WeatherForecast />} />
    							<Route path="weather-forecast" element={<WeatherForecast />} />
    							<Route path="my-weather-stations" element={<WeatherStations />} />
    						</Route>
    						<Route path="about" element={<About />} />
    					</Routes>
    				</Suspense>
    			</MainContent>

Weather.tsx

const Wrapper = styled.div`
	margin-top: 20px;
	max-width: 1200px;
	margin-left: auto;
	margin-right: auto;
	display: grid;
	grid-template-columns: repeat(auto-fit, minmax(100%, 1fr));
	row-gap: 24px;
	ul {
		list-style-type: none;
		display: flex;
		padding: 0;
		margin: 0;
	}
	a {
		all: unset;
		cursor: pointer;
		padding: 12px;
		border: 1px solid #205ad2;
		border-bottom: none;
		border-radius: 8px 8px 0 0;
		&:hover {
			color: white;
			background-color: #205ad2;
		}
		&.active {
			color: white;
			background-color: #205ad2;
		}
	}

	ul li:last-child {
		margin-left: 5px;
	}
`;

    <Wrapper>
    			<nav>
    				<ul>
    					<li>
    						<NavLink to="weather-forecast">Weather forcast</NavLink>
    					</li>
    					<li>
    						<NavLink to="my-weather-stations">My Weather Stations</NavLink>
    					</li>
    				</ul>
    			</nav>
    			<Outlet />
    		</Wrapper>

React-router将索引路由设置为活动v6。

答案1

得分: 1

Update the "/weather" index route to redirect to the "default" "tab" route.

英文:

Update the "/weather" index route to redirect to the "default" "tab" route.

Example:

<Routes>
  <Route path="*" element={<Navigate to="/weather" />} />
  <Route path="weather" element={<Weather />}>
    <Route path="weather-forecast" element={<WeatherForecast />} />
    <Route path="my-weather-stations" element={<WeatherStations />} />
    <Route index element={<Navigate to="weather-forecast" />} />
  </Route>
  <Route path="about" element={<About />} />
</Routes>

React-router将索引路由设置为活动v6。

huangapple
  • 本文由 发表于 2023年4月13日 17:04:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定