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.

getHighlighter throwing error in react project

See original GitHub issue

Pardon my ignorance but I am new here and may not know how to properly post a question.

I am trying to install and use shiki in my react project. I have installed shiki using yarn and am trying to use getHighlighter to highlight my code. But I am facing this weird error

Screenshot 2021-10-14 at 12 41 54 AM

Also facing below error:

Screenshot 2021-10-14 at 12 01 06 AM

Here is my code:

import React from 'react';

const shiki = require('shiki');

componentDidMount () {
  shiki.getHighlighter({
    theme: 'nord'
  }).then((highlighter) => {
    console.log(highlighter.codeToHtml("console.log('shiki');", "js"));
  }).catch((e) => {
    console.log(e);
  });
}

Shiki version: 0.9.12 React version: 16.14.0 Please help me understand this issue and help me resolve this. What am I missing here?

Steps I followed to install and use shiki:

  1. yarn add shiki
  2. Import the module using require('shiki') as mentioned above
  3. Use the imported module directly in the file where it is imported

Thanks.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
patterninacommented, Nov 30, 2021

In React, you have to insert the HTML markup via dangerouslySetInnerHTML.

Here’s a React component sample:

import { useEffect, useState } from 'react'

const shiki = require('shiki')
shiki.setCDN('https://unpkg.com/shiki/')

const CodeBlock = ({ children }) => {
  const [markupToHighlight, setMarkupToHighlight] = useState(null)

  useEffect(() => {
    ;(async function () {
      setMarkupToHighlight(
        await shiki
          .getHighlighter({
            theme: 'nord'
          })
          .then((highlighter) => highlighter.codeToHtml(children, 'swift'))
      )
    })()
  }, [])

  return (
    <div
      dangerouslySetInnerHTML={{ __html: markupToHighlight }}
      className="shiki-wrapper"
    />
  )
}

export default CodeBlock
1reaction
kylebuttscommented, Mar 27, 2022

I’m also looking to try to render server side, but it’s not working with NextJS. Do you have any idea how to fix the above error @pomber?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't load in React Component · Issue #224 · shikijs/shiki
Hi, I'm trying to use Shiki in react component. But it throws an error magic header not detected import React from "react"; import...
Read more >
Error Handling in React 16 – React Blog
Error boundaries catch errors during rendering, in lifecycle methods, and in constructors of the whole tree below them. A class component ...
Read more >
How to Handle Errors in React - AppSignal Blog
Let's look at the various ways you can handle React errors. ... It is a great tool to catch misbehaving code and ensure...
Read more >
How to Handle Errors Efficiently in React - X-Team
catch statement, it's hard to know where the error came from, which line of code made the app break, or which files were...
Read more >
React Error Handling and Logging Best Practices
However, if any code segment throws an error, you must handle it ... only a few understand how to log errors in a...
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