didact icon indicating copy to clipboard operation
didact copied to clipboard

Code breaks

Open CeoFred opened this issue 6 years ago • 6 comments

Copied the whole code for a test and seems to be broken at the point where we try to do the actual work. return ( <h1 onClick={() => setState(c => c + 1)}> Count: {state} )

At this point my browser says Uncaught SyntaxError: Unexpected token '<'at the point we want to render the html as JSX.

CeoFred avatar Nov 15 '19 19:11 CeoFred

For on i feel there should be more to be done before the code works, the browser does not understand Component tags so it keeps throwing errors, i tried solving the issue above by using string literals function Counter() { const [state, setState] = Didact.useState(1) return <h1 onClick=${() => setState(c => c + 1)}> Count: ${state} </h1> } This guy was clearly working till we encountered something else const element = <Counter /> this guy above which clearly threw same syntax errors

CeoFred avatar Nov 15 '19 19:11 CeoFred

No, the browser doesn't understand JSX, as explained in the blog posts, JSX needs to be transformed to function calls (React.createElement or Didact.createElement) by a transpiler (Babel or TS). If you want 0 setup, use parcel bundler to generate code and don't forgot to add:

/** @jsx Didact.createElement */

Before writing/adding JSX

eddyw avatar Nov 17 '19 17:11 eddyw

How do i get started with parcel bundler?

CeoFred avatar Nov 17 '19 18:11 CeoFred

@CeoFred for webpack, @plugins/transform-react-jsx is a must. no idea about parcel :)

cnscorpions avatar Jun 09 '20 02:06 cnscorpions

The simplest way to transform JSX to JS using Babel is:

<div id="root"></div>

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
  // Didact code here
</script>

harryheman avatar Oct 27 '21 09:10 harryheman

The simplest way to transform JSX to JS using Babel is:

<div id="root"></div>

<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script type="text/babel">
  // Didact code here
</script>

yes, this is the simplest way

MuLoo avatar Nov 07 '22 08:11 MuLoo