question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class inside Playgound

See original GitHub issue

Bug Report

Describe the bug

It worked well before New Year 🤷‍♀️, but now I get this error message at build time:

Could not create Open in CodeSandbox { SyntaxError: Unexpected token, expected "}" (21:22)
  19 |       <React.Fragment>
  20 | class CheckboxExample extends React.Component {
> 21 |   constructor(props) {
     |                      ^
  22 |     super(props)
  23 |     this.state = { value: false }
  24 |     this.handleChange = this.handleChange.bind(this)
    at t (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/parser-babylon.js:1:326)
    at Object.parse (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/parser-babylon.js:1:195891)
    at Object.parse$2 [as parse] (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:10600:19)
    at coreFormat (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:13763:23)
    at format (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:13939:16)
    at formatWithCursor (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:13951:12)
    at /Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:41967:15
    at Object.format (/Users/kserjey/Projects/goodwix-frontend/node_modules/prettier/index.js:41986:12)
    at formatter (/Users/kserjey/Projects/goodwix-frontend/node_modules/docz-utils/lib/codesandbox.js:1:411)
    at getSandboxFiles (/Users/kserjey/Projects/goodwix-frontend/node_modules/docz-utils/lib/codesandbox.js:1:1277)
    at Object.getSandboxImportInfo (/Users/kserjey/Projects/goodwix-frontend/node_modules/docz-utils/lib/codesandbox.js:1:1631)
    at addPropsOnPlayground (/Users/kserjey/Projects/goodwix-frontend/node_modules/rehype-docz/dist/index.js:1:974)
  loc: { start: { line: 21, column: 22 } },
  codeFrame:
   '\u001b[0m \u001b[90m 19 | \u001b[39m      \u001b[33m<\u001b[39m\u001b[33mReact\u001b[39m\u001b[33m.\u001b[39m\u001b[33mFragment\u001b[39m\u001b[33m>\u001b[39m\u001b[0m\n\u001b[0m \u001b[90m 20 | \u001b[39m\u001b[36mclass\u001b[39m \u001b[33mCheckboxExample\u001b[39m \u001b[36mextends\u001b[39m \u001b[33mReact\u001b[39m\u001b[33m.\u001b[39m\u001b[33mComponent\u001b[39m {\u001b[0m\n\u001b[0m\u001b[31m\u001b[1m>\u001b[22m\u001b[39m\u001b[90m 21 | \u001b[39m  constructor(props) {\u001b[0m\n\u001b[0m \u001b[90m    | \u001b[39m                     \u001b[31m\u001b[1m^\u001b[22m\u001b[39m\u001b[0m\n\u001b[0m \u001b[90m 22 | \u001b[39m    \u001b[36msuper\u001b[39m(props)\u001b[0m\n\u001b[0m \u001b[90m 23 | \u001b[39m    \u001b[36mthis\u001b[39m\u001b[33m.\u001b[39mstate \u001b[33m=\u001b[39m { value\u001b[33m:\u001b[39m \u001b[36mfalse\u001b[39m }\u001b[0m\n\u001b[0m \u001b[90m 24 | \u001b[39m    \u001b[36mthis\u001b[39m\u001b[33m.\u001b[39mhandleChange \u001b[33m=\u001b[39m \u001b[36mthis\u001b[39m\u001b[33m.\u001b[39mhandleChange\u001b[33m.\u001b[39mbind(\u001b[36mthis\u001b[39m)\u001b[0m' }

When I try to disable codeSandbox: false, I get this message inside <Playground/>:

SyntaxError: Unexpected token (3:21) 1 : (<React.Fragment> 2 : class CheckboxExample extends React.Component { 3 : constructor(props) { ^

And I don’t have any idea how to debug docz. Does it have verbose mode or smth like that? To catch the error message I had to terminate build process via Control+C.

My .mdx file:

---
name: Checkbox
menu: Components
---

import { Playground } from 'docz';
import { Props, Prop } from '../_docz/Props';
import { Checkbox } from './Checkbox';

# Checkbox

Чекбокс

## Props

<Props>
  <Prop name="disabled" type="boolean">
    disabled статус
  </Prop>
  <Prop name="size" type="number">
    Размер чекбокса
  </Prop>
  <Prop name="label" type="string">
    Лэйбл чекбокса
  </Prop>
  <Prop name="onChange" type="function">
    Функция вызываемая при изменении значения чекбокса
  </Prop>
</Props>

## Usage

<Playground>
  {class CheckboxExample extends React.Component {
    constructor(props) {
      super(props);
      this.state = { value: false };
      this.handleChange = this.handleChange.bind(this);
    }

    handleChange(event) {
      // also possible ({ value }) => ({ value: !value })
      this.setState({ value: event.checked });
    }

    render() {
      return (
        <Checkbox label='Hello' value={this.state.value} onChange={this.handleChange}/>
      );
    }
}}
</Playground>

Environment

  • docz: 0.13.7
  • OS: OSX 10.14.2
  • Node: 10.12.0
  • NPM: 6.4.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
douglasnconcommented, Feb 28, 2019

any news on that?

1reaction
alexandre-lelaincommented, Jan 30, 2020

Hello 👋

If it can help the future readers of this issue, using a functional component works (tried with v2.2.0)

<Playground>
  {() => {
    const [counter, setCounter] = React.useState(0)
    return (
      <>
        <button onClick={() => setCounter(counter => counter+1)}>
          Increase
        </button>
        <p>{counter}</p>
      </>
    )
  }}
</Playground>

Note: for some reason, it seems that empty lines is breaking the compiler.

I’d be glad to make a PR to github.com/doczjs/docz-website to add some documentation about this if needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

swift - class in playground vs in program file - Stack Overflow
In real Swift, all executable code must be inside a function (e.g. a method of some class or struct or enum); you can't...
Read more >
Class Playground: Home
Connecting Online Learning with Real World Classrooms. Each page offers concise easy to read summaries of the most commonly taught subjects, concepts, ...
Read more >
Using Classes in the Swift Playground - YouTube
Creating classes in Swift in the Xcode playground and exploring how to use init and convenience init.Table of Contents:01:48 - Declaration ...
Read more >
Nest Playground
We have designed a space where children can play freely and also explore fun classes like gymnastics, dance, and yoga. All class registrations...
Read more >
Indoor playground , themed toddler parties and kids classes ...
Indoor play, classes, community enrichment for children 0-6 years old. Themed birthday parties and special events for families in middle Tennessee.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found