首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试使用我的上下文提供程序文件中的函数

尝试使用我的上下文提供程序文件中的函数
EN

Stack Overflow用户
提问于 2019-07-16 04:30:48
回答 1查看 68关注 0票数 0

我正在使用Context-Api,并试图在生命周期方法中使用从我的文件中提供的函数。当然,函数没有包装在使用者中,所以我查看了文档并设置了上下文的值。这仍然不是working.Everyting在我返回的类component中工作,但是component did挂载不起作用。

代码语言:javascript
复制
import { ProductConsumer } from '../context';

export default class Details1 extends Component
componentDidMount() {
    let value = this.context;
    let id = this.props.match.params.id;
    value.handleDetail(id);
  }

render() {
{value => {
          const {
            id,...} = value.detailProduct;
    return (
      <ProductConsumer>
        {value => {
My Component
 </ProductConsumer>

 export const Details = () => (
  <Product.Consumer>
    {context =>
      <Details1 context={context}/>

    }
  </Product.Consumer>
)
EN

回答 1

Stack Overflow用户

发布于 2019-07-16 04:33:26

您可以使用消费者包装组件,将函数作为道具传递给它,或者(更好地-)将组件转换为功能组件,使用useContext挂钩从上下文中获取值。

代码语言:javascript
复制
import React, { useContext } from "react"; 
import someContext from "./context-path";

const MyComponent = () => {
  const { myFunction } = useContext(someContext);
  ...
};

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57046782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档