javascript - 在组件中传递 ID
问题描述
我正在开发一个应用程序,但是我目前被困在如何解决这个问题上。
import React from "react"
import { Switch } from "react-router-dom"
import LandingPage from "./LandingPage/"
import Dashboard from "./pages/Dashboard"
import CustomRoute from "../utils/CustomRoute"
import Pages from "./pages/Pages"
import PublicTrip from "./Maps/singleTripPublic"
const Root = () => (
<Switch>
<CustomRoute path="/" exact component={LandingPage} />
<CustomRoute path="/app" protectedPath component={Dashboard} />
<CustomRoute
path="/public/:tripId"
render={({ match }) => <PublicTrip tripId={match.params.tripId} />}
/>
<CustomRoute path="/" component={Pages} />
<CustomRoute render={() => <div>404: Route not found</div>} />
<Pages />
</Switch>
)
export default Root
这是我的root.js,我想知道如何将tripId 传递给Link 标签,以便在单击链接时呈现public/“tripId”。如果向下滚动,您可以看到带有tripId 的标签。如何传入tripId,以便在我单击时实际重定向到它。任何帮助,将不胜感激。谢谢。
import React from "react"
import * as s from "./components"
import { connect } from "react-redux"
import moment from "moment"
import PropTypes from "prop-types"
import { TripPropTypes } from "../../propTypes"
import { Button } from "../../../styles/theme/styledComponents"
import { toggleWaypoint } from "../../../redux/actions/trips"
import marker from "../../icons/orange-marker.svg"
import startMarker from "../../icons/green-marker.svg"
import endMarker from "../../icons/black-marker.svg"
import { Link } from "react-router-dom"
class ActiveTripPanel extends React.Component {
constructor(props) {
super(props)
this.state = {
polylines: null,
markers: []
}
}
componentDidMount() {
setTimeout(() => {
this.renderWaypoints()
this.drawPolylines()
}, 500)
}
componentDidUpdate(prevProps) {
if (prevProps.waypoints !== this.props.waypoints) {
this.renderWaypoints()
this.drawPolylines()
}
}
drawPolylines = () => {
if (this.state.polylines !== null) {
this.state.polylines.active.setMap(null)
this.state.polylines.complete.setMap(null)
this.state.polylines.current.setMap(null)
}
let completeIndex = 0
for (let i = 0; i < this.props.waypoints.length; i++) {
if (!this.props.waypoints[i].complete) {
completeIndex = i
break
}
}
const completed = this.props.waypoints.slice(0, completeIndex)
const active = this.props.waypoints.slice(
completeIndex,
this.props.waypoints.length + 1
)
const current = this.props.waypoints.slice(
completeIndex - 1,
completeIndex + 2
)
const completePath = completed.map(waypoint => {
return { lat: waypoint.lat, lng: waypoint.lon }
})
const activePath = active.map(waypoint => {
return { lat: waypoint.lat, lng: waypoint.lon }
})
const currentPath = current.map(waypoint => {
return { lat: waypoint.lat, lng: waypoint.lon }
})
const completePolyline = new window.google.maps.Polyline({
path: completePath,
strokeColor: "#FF0000",
strokeOpacity: 1.0,
strokeWeight: 2
})
const currentPolyline = new window.google.maps.Polyline({
path: currentPath,
strokeColor: "#008000",
stokeOpacity: 1.0,
stokeWeight: 2
})
const activePolyline = new window.google.maps.Polyline({
path: activePath,
strokeColor: "#000000",
strokeOpacity: 1.0,
strokeWeight: 2
})
completePolyline.setMap(window.map)
activePolyline.setMap(window.map)
currentPolyline.setMap(window.map)
this.setState({
polylines: {
active: activePolyline,
complete: completePolyline,
current: currentPolyline
}
})
}
renderWaypoints = () => {
let markers = []
const baseIcon = {
anchor: new window.google.maps.Point(15, 30),
scaledSize: new window.google.maps.Size(30, 30),
labelOrigin: new window.google.maps.Point(15, 13)
}
const icons = {
start: {
url: startMarker,
...baseIcon
},
end: {
url: endMarker,
...baseIcon
},
marker: {
url: marker,
...baseIcon
}
}
this.props.waypoints.map((item, i) => {
const icon =
i === 0
? icons.start
: i === this.props.waypoints.length - 1
? icons.end
: icons.marker
let center = { lat: item.lat, lng: item.lon }
const marker = new window.google.maps.Marker({
position: center,
map: window.map,
icon,
title: item.name,
label: {
text: `${i + 1}`,
color: "white",
fontFamily: "Wals",
fontWeight: "bold"
}
})
markers.push(marker)
})
}
render() {
const publicId = ({ match })
return (
<s.Panel>
{/* <s.PanelHeader>{this.props.trip.name}</s.PanelHeader>
<s.DateLabel>
Start: {moment(this.props.trip.start).format("YYYY-MM-DD")} - End:{" "}
{moment(this.props.trip.end).format("YYYY-MM-DD")}
</s.DateLabel> */}
<Link to="/public/{match.params.tripId}">Share Trip</Link>
<s.WaypointTracker>
{this.props.waypoints &&
this.props.waypoints.map(waypoint => (
<s.WaypointStepper key={waypoint.id}>
<div>
<h4>{waypoint.name}</h4>
<div>
ETA: {moment(waypoint.start).format("YYYY-MM-DD HH:mm")}
</div>
<div>
Status: Checked In @{" "}
{moment(waypoint.start).format("HH:mm")}
</div>
</div>
<div>
{waypoint.complete ? (
<Button
onClick={() => this.props.toggleWaypoint(waypoint.id)}
>
<i className="fa fa-check" />
</Button>
) : (
<Button
onClick={() => this.props.toggleWaypoint(waypoint.id)}
>
<i className="fa fa-times" />
</Button>
)}
</div>
</s.WaypointStepper>
))}
</s.WaypointTracker>
</s.Panel>
)
}
}
ActiveTripPanel.propTypes = {
trip: TripPropTypes,
waypoints: PropTypes.array.isRequired,
toggleWaypoint: PropTypes.func.isRequired
}
const mapStateToProps = ({ trips }) => ({
trip: trips.activeTrip,
waypoints: trips.activeTrip && trips.activeTrip.waypoints
})
export default connect(
mapStateToProps,
{ toggleWaypoint }
)(ActiveTripPanel)
[编辑]
自定义路由代码
import React from "react"
import { connect } from "react-redux"
import { Redirect, Route } from "react-router"
import { addTokenToState } from "../redux/actions/auth"
const CustomRoute = props => {
const { isLoggedIn, protectedPath, checkedForToken, ...rest } = props
// If not logged in and haven't checked for token yet,
// try to query DB for user with token:
if (!checkedForToken && !isLoggedIn) {
props.addTokenToState()
}
if (isLoggedIn || !protectedPath) {
return <Route {...rest} />
}
if (protectedPath && !isLoggedIn) {
return (
<Redirect
to={{
pathname: "/login",
state: { from: props.path }
}}
/>
)
}
}
const mapStateToProps = state => ({
isLoggedIn: state.auth.isLoggedIn,
checkedForToken: state.auth.checkedForToken
})
const mapDispatchToProps = { addTokenToState }
export default connect(
mapStateToProps,
mapDispatchToProps
)(CustomRoute)
解决方案
我希望这有帮助
<Link to="/public/${match.params.tripId}">Share Trip</Link>
推荐阅读
- html - DOM完全渲染后如何显示按钮
- php - 不在本地主机中打开文件夹
- date-fns - date-fns:parseISO(string) 和 new Date(string) 之间的区别?
- java - 没有得到结果:使用 completableFuture
- sql - 如何在 PrestoDB 中创建一个具有持久值的计算列?
- odata - 如何在自定义 oData 服务中创建 GENERATED_ID 以提供分析表?
- react-native - react-native-audio-toolkit,isPlaying 不起作用
- python - 尝试打印 csv 文件时出现新的 python NameError
- opendaylight - Opendaylight 与 gns3 的集成
- bash - 加载大文件(例如:40M)后,“pwd”命令需要花费大量时间