简体   繁体   English

React Context API Consumer块中的调试器 - 这是未定义的

[英]Debugger inside React Context API Consumer block - this is undefined

I am trying to debug a component using Context API Consumer inside render method on browser dev tools. 我正在尝试使用浏览器开发工具中的render方法中的Context API Consumer来调试组件。 If i place break-point inside Consumer block, i can't print props etc. on console dynamically as this is undefined . 如果我在Consumer块中放置断点,我不能动态地在控制台上打印道具等,因为thisundefined Normally running this works fine, but while debugging only value of this is undefined . 通常运行它工作正常,但在调试时只有this值是undefined Following is sample render method of component. 以下是组件的示例呈现方法。

  componentMethod = () => {
      console.log(this.props.name); //Placing breakpoint here, value is this is defined
  }

  render() {
        return (
            <div className={styles.container}>
                <div>
                    <h4>{this.props.name}</h4>
                </div>
                <div className={styles.block}>
                    <MyComponent.Consumer>
                        {
                            ({ firstParam, secondParam }) =>
                                <AotherComponent
                                    firstParam={firstParam}
                                    secondParam={secondParam}
                                    thirdParam={this.props.name}
                                />
                        }
                    </MyComponent.Consumer>
                </div>
            </div>
        )
  }

I could be related fat arrow use here, but I am able to get value of this while using break-point in componentMethod . 我可以在这里使用胖箭头,但是我可以在componentMethod使用断点来获得这个值。 Is there a way to bind this for Consumer block? 有没有办法将它bind this Consumer块?

Try this, however, your question doesn't give enough context on what you are trying to solve. 试试这个,但是,你的问题没有提供你想要解决的问题的足够背景。 It would be better if you shared the Provider implementation as well and where you use it. 如果您共享Provider实现以及使用它的位置会更好。

render() {
        const { name } = this.props;
        return (
            <div className={styles.container}>
                <div>
                    <h4>{name}</h4>
                </div>
                <div className={styles.block}>
                    <MyComponent.Consumer>
                        {
                            ({ firstParam, secondParam }) =>
                                <AotherComponent
                                    firstParam={firstParam}
                                    secondParam={secondParam}
                                    thirdParam={name}
                                />
                        }
                    </MyComponent.Consumer>
                </div>
            </div>
        )
  }

It looks like you are interested in knowing what your consumer is passing down to your component during execution. 看起来您有兴趣了解您的消费者在执行期间传递给您的组件的内容。 There are two ways to accomplish it. 有两种方法可以实现它。

Hard Way 艰辛的道路

Let us breakdown how the consumer works (using your sample). 让我们分析消费者的工作方式(使用您的样本)。 That may help you with finding the right place to debug. 这可能有助于您找到正确的调试位置。

in your render() method, you have a <MyComponent.Consumer> call. render()方法中,您有一个<MyComponent.Consumer>调用。 The new Context Consumer API is built on the render-prop pattern . 新的Context Consumer API构建在render-prop模式上

Important thing to remember about the render-prop pattern is that: it is a function call. 关于render-prop模式需要记住的重要一点是:它是一个function调用。 This function should return something which react can consider while rendering the tree. 此函数应返回在渲染树时可以考虑的内容。

Since it is a function call, you can put your console.log statements before your element. 由于它是一个函数调用,您可以将console.log语句放在元素之前。 You will have to add an explicit return statement though. 您必须添加一个显式的return语句。

As to why it is undefined in your method. 至于为什么它在你的方法中是undefined的。 I am assuming that the componentMethod is not a lifecycle method, so it is possible that this or this.props is undefined based on how you are invoking that method. 我假设componentMethod不是生命周期方法,因此有可能是thisthis.props是基于你如何调用该方法不明确的。 I do not see it invoked anywhere in your render method. 我没有在渲染方法中的任何地方看到它被调用。

Eas(y/ier) Way: Eas(y / ier)方式:

Use react dev tools browser extension. 使用react dev工具浏览器扩展。 You can look up all components by name . 您可以按名称查找所有组件。 On clicking them you can see the props and state and even the context (as far as I remember). 点击它们你可以看到道具和状态,甚至是上下文(据我记得)。 You can even change the values and see react react to it! 你甚至可以改变的值,看看反应的反应吧!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM