简体   繁体   English

我如何从道具访问我的反应组件中的历史记录?

[英]how can i access the history in my react components from props?

so i'm trying to get the history object from the props in the component i'm working on and it always prints an error this is the class i'm trying to use history object in it所以我试图从我正在处理的组件中的道具中获取历史 object 并且它总是打印错误这是 class 我正在尝试在其中使用历史 object

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>
);

} }

export default Admin;导出默认管理员;

and the app.jsx i called it in route like this和 app.jsx 我在这样的路线中调用它

                            <Route path="/admin" element = {<Admin
                        prod = {this.state.Product}
                        delete={this.deleteEntery}
                        editing = {this.editElement}
                        />}/>

You have not imported useHistory hook and declared history using it.您尚未导入 useHistory 钩子并使用它声明历史。

Add this to import:将此添加到导入:

import { useHistory } from 'react-router-dom'

Declare history inside your function:在 function 中声明历史记录:

const history = useHistory()

First thing you are not passing 'history' as a prop to Admin component, if you wanted to use history object in your component you would simply need to import it and use it (No need to pass it as prop).首先,您没有将“历史”作为道具传递给管理组件,如果您想在组件中使用历史 object,您只需导入它并使用它(无需将其作为道具传递)。

// 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问从 API 获取的 object 属性,并将它们传递到我的 React 组件中? - How can i access object properties taken from an API, and pass them into my components in React? 如何为功能性React组件props实施解构分配? - How can I implement destructuring assignment for functional react components props? 如何使用道具将 url 提供给反应组件? - How can i give a url to a react components using props? 如何从 React 中的父级访问子组件中的变量? - How can I access variables in children components from parent in React? 如何将道具传递给来自不同来源的组件? - How can I pass props to components from different sources? 如何从类外部的fineuploader const回调访问我的react组件props? - How do I access my react component props from a fineuploader const callback outside the class? 我怎样才能解构一个 React 道具并仍然访问其他道具? - How can I destructure a React prop and still access other props? 我如何访问传递给React.Component的道具 - How can I access props passed to React.Component 如何使用反应测试库测试使用 props.history 的 onClick 按钮? - How can I test a button which uses props.history for it's onClick with react testing library? 如何访问componentWillMount中的历史道具? - How to access history props in componentWillMount?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM