Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'querySelectorAll' of null #39

Closed
Jarvis1010 opened this issue Sep 27, 2017 · 25 comments
Closed

TypeError: Cannot read property 'querySelectorAll' of null #39

Jarvis1010 opened this issue Sep 27, 2017 · 25 comments

Comments

@Jarvis1010
Copy link

Upgraded to React 16 last night and now the react highlight throws the above error

@briancappello
Copy link
Contributor

Until a fix gets merged, I've uploaded a branch to my fork that includes the compiled assets. It can be installed like so from package.json:

"dependencies": {
    // ...
    "react-highlight": "briancappello/react-highlight#react-v16-compiled"
}

@mlynch
Copy link

mlynch commented Oct 11, 2017

@briancappello Thanks for doing that. Unfortunately still seeing issues using your branch. Will add more if I find anything:

screen shot 2017-10-11 at 1 15 02 pm

@briancappello
Copy link
Contributor

@mlynch Can you share what your code looks like when you're using the Highlight component? I suspect the error you're seeing is related to using a custom element prop, which as best I could tell isn't documented and therefore I just guessed (apparently wrongly :-O) at what kinds of inputs it was supposed to work with.

MedicineYeh added a commit to snippits/snippit-ui that referenced this issue Oct 16, 2017
TODO: React recently changes its API version. Some packages
are still migrating. Need to be checked later.

Fix "react-highlight":
"github:briancappello/react-highlight#react-v16-compiled"
akiran/react-highlight#39
@mlynch
Copy link

mlynch commented Oct 16, 2017

@briancappello here's how it's being used:

screenshot 2017-10-16 12 51 43

Where context is a rendered JSX sub-tree with some divs for each line

@briancappello
Copy link
Contributor

@mlynch Interesting, looks to me like it should be working :/

Just to verify the bug really is somewhere in my PR, can you confirm the same code worked with React 0.15, and that no error happens if you do this with React 0.16?:

{context && (
<pre>
  <code className="error-mapped-context typescript">
    {context}
  </code>
</pre>
)}

(It won't render highlighted, but it also shouldn't blow up)

@mlynch
Copy link

mlynch commented Oct 16, 2017

@briancappello thanks. Yep this worked in React 15, and changing the 16 version to your code above makes it work without issue (no highlighting, of course)

@briancappello
Copy link
Contributor

@mlynch Hrmph. I'm sad to say I'm at a loss on this one :(

@chaoyangnz
Copy link

++++++111111

@richburdon
Copy link

Broken for me in 16 also.

@davidthornton
Copy link

Would be nice to have a fix merged

@alechstong
Copy link

@briancappello Thanks for the good work. I am using your branch. everything works except the innerHTML=true option, which doesn't seems to do anything. I managed to find a workaround with html-react-parser, but it is very ugly.

@briancappello
Copy link
Contributor

@alechstong I'm not sure I follow what you needed a workaround for? The innerHTML option is for when you want to highlight a block of trusted, pre-rendered HTML (ie not JSX, but a string of HTML from the backend).

So, without innerHTML, this works (it wraps the children in <pre><code></code></pre>):

<Highlight language="javascript">
  {`function foo() { return 'bar' }`}
</Highlight>

Whereas with innerHTML, there is no automatic wrapping:

<Highlight innerHTML languages={['python', 'javascript']}>
  {`<p>This is a string of HTML. It has multiple blocks of code within it.</p>
   <pre><code>function foo() { return 'like this one' }</code></pre>
   <pre><code>def foo():\n    return 'and this one'</code></pre>
  `}
</Highlight>

@alechstong
Copy link

@briancappello Thank you for replying. I tried your snippets, they do work. And I tried to reproduce the problem in my project, but I'm glad to see the problem's gone now. I remember that the innerHTML attrib didn't work for us, so we had to convert the string to react DOM and manually replace the code tag with Highlight. Must be something we did wrong.

@romainbessugesmeusy
Copy link

Running React 16.2.0, app created with create-react-app, same error here. Seems to break here :

    highlightCode() {
        const domNode = ReactDOM.findDOMNode(this);
        const nodes = domNode.querySelectorAll('pre code');

@ajGingrich
Copy link

I also am having problems in React 16.2 with error Uncaught TypeError: Cannot read property 'querySelectorAll' of null at Highlight.highlightCode (index.js:52) at Highlight.componentDidMount

@discoduke
Copy link

I'm also having a problem with this in React 16.2. As far as I can tell, both React.DOM.div and ReactDOM.findDOMNode are now deprecated in React 16. The preferred method is to use {ref} in the render method to store a reference to specific element.

As far as I can tell 'Highlight innerHTML' is still working as expected -- I can see the pre/code elements in the original page source. But when componentDidMount/highlightCode executes (after page load), ReactDOM.findDOMNode(this) returns null.

@briancappello
Copy link
Contributor

@discoduke I believe you are correct. I opened a PR (#43) a while ago that drops the deprecated stuff and uses ref instead. Sadly, I've heard nothing but radio silence from @akiran. See my comment above if you want to try it out.

@gihrig
Copy link

gihrig commented Jan 5, 2018

@briancappello Your patch works for me! 🎉

I'm following this Learn Next JS lesson.

I hope @akiran is OK...

@devXela
Copy link

devXela commented Jan 8, 2018

Thanks @briancappello!

@znafets
Copy link

znafets commented Jan 19, 2018

Thanks @briancappello! Works like a charm.

"dependencies": {
    "cross-env": "^5.0.1",
    "marked": "^0.3.6",
    "next": "^4.2.0",
    "react": "^16.2.0",
    "react-dom": "^16.2.0",
    "react-highlight": "briancappello/react-highlight#react-v16-compiled",
    "webpack-bundle-analyzer": "^2.8.3"
  }

@RahavLussato
Copy link

@mlynch try to change the require from const Highlight = require('react-highlight');
to import Highlight from 'react-highlight';

@adammenges
Copy link

Hitting this now too, trying @briancappello's patch.

@johngrimes
Copy link

Here's what I did when I encountered this error:

  1. Went to https://highlightjs.org/download/ and got the minimal package of files for what I need (which was much smaller than what react-highlight was bringing in).
  2. Extracted this into src/vendor/highlight.
  3. Used it within my React component something like this:
import Highlight from './vendor/highlight'

class SomeComponent extends Component {
  constructor(props) {
    super(props)
    this.highlightCode = this.highlightCode.bind(this)
  }

  componentDidMount() { this.highlightCode() }
  componentDidUpdate() { this.highlightCode() }

  highlightCode() {
    if (this.code) Highlight.highlightBlock(this.code)
  }

  render() {
    return <pre className="json" ref={el => { this.code = el }}>{this.props.someCode}</pre>
  }
}

Then I just removed the dependency on react-highlight!

Anybody see any downsides to this approach? I can't see that this module is really buying me much at this point.

@Ajk4
Copy link

Ajk4 commented Mar 10, 2018

Thx @johngrimes for snippet, I hit this problem too and you snippet is helpful.

@mbj36
Copy link
Contributor

mbj36 commented Mar 14, 2018

We have published new version with React 16 support

@mbj36 mbj36 closed this as completed Mar 14, 2018
mbautin pushed a commit to mbautin/yugabyte-db that referenced this issue Jul 16, 2019
…after React16 upgrade

Summary:
Fix as proposed in
akiran/react-highlight#39 (comment)
{F8445}

Test Plan:
Go to failed task view. Task failure detail should load.

Go to OnPrem JSON view. JSON view should load.

Reviewers: vit.pankin, ram

Reviewed By: ram

Subscribers: jenkins-bot, yugaware

Differential Revision: https://phabricator.dev.yugabyte.com/D4198
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests