así que estoy tratando de obtener el objeto de historial de los accesorios en el componente en el que estoy trabajando y siempre muestra un error. Esta es la clase en la que estoy tratando de usar el objeto de historial.
import React from 'react'; const Admin = (props) => { return ( <React.Fragment> <h1>Admin</h1> <button onClick={()=>this.props.history.push("/edit/new")} type="button" className="btn btn-primary btn-lg m-1"> Add </button> <table className="table"> <thead> <tr> <th scope="col">Name</th> <th scope="col">Price</th> <th></th> <th></th> </tr> </thead> <tbody> {props.prod.map(prod1 =>( <tr key ={prod1.id}> <td>{prod1.name}</td> <td>{prod1.price}</td> <td> <i onClick={()=>this.props.history.push(`/edit/${prod1.id}`) } className="fas fa-edit"></i> </td> <td> <span onClick={()=>props.delete(prod1)}> <i className ="fas fa-trash m-2" ></i> </span> </td> </tr> ))} </tbody> </table> </React.Fragment> );}
exportar administrador predeterminado;
y app.jsx lo llamé en una ruta como esta
<Route path="/admin" element = {<Admin prod = {this.state.Product} delete={this.deleteEntery} editing = {this.editElement} />}/>No ha importado el enlace useHistory y ha declarado el historial usándolo.
Agregue esto para importar:
import { useHistory } from 'react-router-dom'llama al historial de useHistory dentro de tu función usando el historial de variables:
const history = useHistory() Si tiene algún error de importación, use la última versión de React e instale 'react-router-dom' v5.2.0 usando este comando: npm install 'react-router-dom@v5.2.0'
En primer lugar, no está pasando 'historial' como accesorio al componente de administración, si quisiera usar el objeto de historial en su componente, simplemente necesitaría importarlo y usarlo (no es necesario pasarlo como accesorio).
// Import it at the top of your file import { useHistory } from 'react-router-dom' // Call this hook inside your functional component, history variable has all the required properties const history = useHistory()