【问题标题】:Debug react JSX in vscode在 vscode 中调试 React JSX
【发布时间】:2019-02-14 07:46:11
【问题描述】:

我正在创建一个 React 应用程序,我需要在特定点查看 JSX,所以我开始使用 VSCode 进行调试,我的代码如下:

 render(): JSX.Element {
        let result =  (
            <div className="footerComponent">
                <DefaultButton className={commonStyles.defaultButton} onClick= {() => this.props.saveAllSettings()} >Save Changes</DefaultButton>
                <DefaultButton className="" onClick = {() => this.setState({ dialogOpened: true})}>Discard Changes</DefaultButton>
                { this.state.dialogOpened && this.props.discardChangesDialog }
            </div>
        )
        return result
    }

我在该行设置了一个断点:return result,并在 vscode 的调试控制台中记录了该值,但它显示了这个:

Object {$$typeof: Symbol(react.element), type: "div", key: null, ref: null, props: Object, …}

如何查看确切的 JSX?基本上,我想知道表达式{ this.state.dialogOpened &amp;&amp; this.props.discardChangesDialog } 返回什么。

这是我在“监视”窗口中看到的内容:

谢谢。

【问题讨论】:

    标签: reactjs visual-studio-code jsx


    【解决方案1】:
    render(): JSX.Element {
    debugger;
    this.state.dialogOpened;
    this.props.discardChangesDialog
    let result =  (
        <div className="footerComponent">
            <DefaultButton className={commonStyles.defaultButton} onClick= {() => this.props.saveAllSettings()} >Save Changes</DefaultButton>
            <DefaultButton className="" onClick = {() => this.setState({ dialogOpened: true})}>Discard Changes</DefaultButton>
            { this.state.dialogOpened && this.props.discardChangesDialog }
        </div>
    )
    return result
    }
    

    只需添加一个调试器,然后打开控制台。

    【讨论】:

    • 我已经在调试器会话中。正如我所说,我停在断点处,但结果的值不是我期望调试的 JSX 格式。
    • 好的,所以添加该值作为手表,希望这会有所帮助。 developers.google.com/web/tools/chrome-devtools/javascript/…
    • 我已经这样做了.. 如果您阅读了我的问题...该值就在那里,但没有像人们期望的那样格式化为 JSX。请再次阅读我的问题..
    • I want to know what the expression { this.state.dialogOpened &amp;&amp; this.props.discardChangesDialog },使用 watch 可以评估表达式值。为了查看它在 html 中的样子,您可能正在寻找 reactjs.org/docs/react-dom-server.html#rendertostring
    • 请检查我更新的问题中的图像。手表基本上显示相同。
    猜你喜欢
    • 2020-01-21
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 2023-01-09
    • 2021-03-09
    • 1970-01-01
    • 2019-10-12
    相关资源
    最近更新 更多