[英]Can I put class base components inside function base components to use react hooks?
I have an app that was written mainly with class base react components, however we are implementing a paywall feature with Stripe.我有一个主要使用 class 基本反应组件编写的应用程序,但是我们正在使用 Stripe 实现付费专区功能。
The issue is stripe uses hooks, which don't work with class base components.问题是条纹使用钩子,它不适用于 class 基本组件。 Is it possible to put a child class base component into a parental function base component in order to achieve this.
是否可以将子 class 基本组件放入父 function 基本组件中以实现此目的。 How would I go about doing this without rewriting every component as a function base component.
在不将每个组件重写为 function 基本组件的情况下,我将如何 go 做到这一点。
Every attempt to include a function base component with class base children always yields this error:每次尝试将 function 基本组件与 class 基本子组件包含在一起总是会产生此错误:
Error: Invalid hook call.错误:无效的挂钩调用。 Hooks can only be called inside of the body of a function component.
钩子只能在 function 组件的主体内部调用。
Are there any work arounds, or should I re-write the all the components as functional components?是否有任何解决方法,或者我应该将所有组件重新编写为功能组件?
A quick example using react-router-dom for query parameters, then passing those into a class base child component.一个使用 react-router-dom 查询参数的快速示例,然后将这些参数传递到 class 基础子组件。
function useQuery() {
return new URLSearchParams(useLocation().search);
}
const App = () => {
let query = useQuery();
return (
<Provider store={store}>
<Router>
<div className="App">
<Navbar />
<Switch>
{/* Public Pages */}
<Route exact path="/" component={Landing} ref={query.get('ref')} />
</Switch>
</div>
</Router>
</Provider>
)
}
export default App;
Then assuming that the landing components is a simple class base component that will set the prop into the state.然后假设着陆组件是一个简单的 class 基础组件,它将把 prop 设置到 state 中。
Component's type doesn't matter in relation parent-child.组件的类型与父子关系无关。 But you can't use hooks inside of classBased component.
但是你不能在 classBased 组件中使用钩子。 Only in functional ones
仅在功能性
Here is the code example of Functional Component as Parent and Class as Child这是功能组件作为父级和 Class 作为子级的代码示例
import React , {Component}from "react";
import "./styles.css";
const App = () => {
return (
<div className="App">
<h1>I Am Parent Functional Component</h1>
<Child />
</div>
);
};
export default App;
class Child extends Component
{
render(){
return(
<h2>I am Class Child Component</h2>
)
}
}
Okay I figured out the issue with this problem.好的,我发现了这个问题的问题。 Since I found the same question unanswered on the internet, I decided to answer the question myself, Just for future reference for anyone looking for the same solution I was running into.
由于我在互联网上发现同样的问题没有得到解答,所以我决定自己回答这个问题,仅供任何寻求我遇到的相同解决方案的人将来参考。
You can use Hooks on a parental Function Base Component and pass on the information into a child base component.您可以在父 Function 基础组件上使用 Hook,并将信息传递到子基础组件中。
While rendering the application, I was given an error渲染应用程序时,出现错误
Error: Invalid hook call. Hooks can only be called inside of the body of a function component.
However this error was due to outdated modules in npm.但是,此错误是由于 npm 中的过时模块造成的。 I just updated the react-router-dom from 4.1 to 5.2.0
npm update react-router-dom
and it seem to solve the issue of using functional base component as a parent.我刚刚将 react-router-dom 从 4.1 更新到 5.2.0
npm update react-router-dom
似乎解决了使用功能基础组件作为父级的问题。
After updating the module I was able to use a hooks in a parental function base component and pass that on to a child class base component using props.更新模块后,我能够在父 function 基本组件中使用挂钩,并使用道具将其传递给子 class 基本组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.