简体   繁体   English

我如何通过此错误无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 我正在使用 React18

[英]How do i get pass this error Invalid hook call. Hooks can only be called inside of the body of a function component. i am using React18

this is the code这是代码

import { connect } from "react-redux";
import { useParams } from "react-router-dom";
import ExpenseForm from "./ExpenseForm";
import { editExpense, removeExpense } from '../actions/expenses';
import {useNavigate} from 'react-router-dom';
//import { withRouter } from "react-router-dom";



function EditExpensePage (props){
 const { id } = useParams();
const navigate = useNavigate()
 return(
   <div>
           <ExpenseForm 
           expense={props.expense}
           onSubmit={(expense) =>{
             props.dispatch(editExpense(id,expense))
             navigate('/');
           }}
           />

         <button onClick={()=>{
          props.dispatch(removeExpense({id: props.expense.id}));
          navigate('/');
       }}>Remove</button>
         </div>

         
 );
};

function MapStateToProps(state){
 const { id } = useParams();
 return{
   expense: state.expenses.find((expense)=> expense.id ===  id)
 };
};

   

  export default connect(MapStateToProps)(EditExpensePage);

i am trying to edit an expense with an id so that it can render a component that has the edited item.我正在尝试使用 id 编辑费用,以便它可以呈现具有已编辑项目的组件。 so far everything is working but after editing the field the component doesnt rerender, it return an error on the console.到目前为止一切正常,但在编辑该组件不重新呈现的字段后,它会在控制台上返回一个错误。 this is the error Invalid hook call.这是错误无效的挂钩调用。 Hooks can only be called inside of the body of a function component钩子只能在 function 组件的主体内部调用

The reason for the error is you have used useParams inside MapStateToProps which you can not use inside MapStateToProps .错误的原因是您在MapStateToProps中使用useParams ,而在MapStateToProps中无法使用。 You can do something like this:你可以这样做:

function MapStateToProps(state,ownProps){
 const { id } = ownProps.match.params;
 return{
   expense: state.expenses.find((expense)=> expense.id ===  id)
 };
};

暂无
暂无

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

相关问题 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 通过路由反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. by Routing in react 错误:无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 反应 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. - React 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 使用 React.js 时 - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. while using React.js 收到此错误“无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。” 在反应功能组件内? - Getting this error “Invalid hook call. Hooks can only be called inside of the body of a function component.” inside a react functional component? 错误:无效的挂钩调用。 钩子只能在函数组件的主体内部调用。 (带反应带) - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. (w/ Reactstrap) 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 使用 state 时 - Invalid hook call. Hooks can only be called inside of the body of a function component. - When using state 无效的钩子调用。 钩子只能在函数组件的主体内部调用。 如何解决这个问题? - Invalid hook call. Hooks can only be called inside of the body of a function component. How to solve this issue? React - 错误:无效的钩子调用。 钩子只能在 function 组件的主体内部调用。 我的语法有什么问题? - React - Error: Invalid hook call. Hooks can only be called inside of the body of a function component. What is wrong my syntax? 我不断收到:错误:无效的挂钩调用。 Hooks 只能在函数组件内部调用 - I keep getting:Error: Invalid hook call. Hooks can only be called inside of the body of a function component 无效的挂钩调用。 钩子只能在 function 组件的主体内部调用。 - 使用上下文 - Invalid hook call. Hooks can only be called inside of the body of a function component. - useContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM