我正在开发一个应用程序,但是目前我对如何解决这个问题感到困惑。
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)
[编辑]
CustomRoute代码
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)
答案 0 :(得分:1)
我希望这对您有帮助
<Link to="/public/${match.params.tripId}">Share Trip</Link>
答案 1 :(得分:0)
更改
<Link to="/public/{match.params.tripId}">Share Trip</Link>
对此
<Link to={`/public/${this.props.match.params.tripId}`}>Share Trip</Link>
更新
如果要访问组件中的match
对象,则必须像这样传递match
<CustomRoute
path="/public/:tripId"
render={({ match }) => <PublicTrip match={match} />}
// or this render={props => <PublicTrip {...props} />}
/>
在您的代码中,您仅传递了tripId
,这意味着您无法访问match
对象,您可以像这样tripId
获得this.props.tripId
所以您的链接应该是这样
<Link to={`/public/${this.props.tripId}`}>Share Trip</Link>
实际上,您甚至不使用渲染来获得匹配参数,就像这样
<CustomRoute path="/public/:tripId" component={PublicTrip} />
然后Route
会将match
注入您的ActiveTripPanel
,因此您可以像这样ActiveTripPanel
在this.props.match.params
中获得匹配参数: