我有一条路线<Route path="post/:id" element={<PostByIdPage />} />
我怎样才能id像propsreact router dom v6一样获取参数?
这是我的 PostByIdPage 组件:
import React, { Component } from 'react';
import OneSecret from '../OneSecret';
import getSecretsPublic from '../../../action/getSecretsPublic';
import { connect } from 'react-redux';
export class index extends Component {
constructor(props) {
super(props);
this.state = {
secretPublic: {
content: ''
}
}
let id = decodeURI(window.location.pathname.substring(6));
let params = {
id: id
}
this.props.getSecretsPublic(params)
.then(res =>{
if (res.type === 'ERROR_MESSAGE') {
return;
}
this.setState({secretPublic: res.payload.data[0]}) …Run Code Online (Sandbox Code Playgroud) 我正在使用react-router-dom v6,不知道如何获取参数值
前任:http://localhost:3000/user/:id
我想要得到:id
有人使用钩子useParams来获取,但我正在使用类,所以我不能使用钩子。