Giter Club home page Giter Club logo

Comments (14)

Salakar avatar Salakar commented on August 16, 2024 65

@method-x @sslotsky had the same issue here using next.js and SSR - I've worked around this issue by importing as follows:

let CodeMirror = null;
if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
  CodeMirror = require('react-codemirror');
  require('codemirror/mode/yaml/yaml');
  require('codemirror/mode/dockerfile/dockerfile');
}

And then rendering:

// render
   { CodeMirror && <CodeMirror ... /> }

After which it's only the client now that renders, on the server it's ignored. Does the job 👍

from react-codemirror.

songyule avatar songyule commented on August 16, 2024 9

if you are using NextJS, maybe you can use this easier way

import dynamic from 'next/dynamic';
const CodeMirror = dynamic(() => import('react-codemirror'), { ssr: false });

from react-codemirror.

sombreroEnPuntas avatar sombreroEnPuntas commented on August 16, 2024 9

if you are using NextJS, maybe you can use this easier way

import dynamic from 'next/dynamic';
const CodeMirror = dynamic(() => import('react-codemirror'), { ssr: false });

Something like this should work, @prashantchothani

import React, { useState } from 'react'
import dynamic from 'next/dynamic'

import 'codemirror/lib/codemirror.css'

const CodeMirror = dynamic(() => {
  import('codemirror/mode/javascript/javascript')
  return import('react-codemirror')
}, { ssr: false })

export const CodeEditor = () => {
  const [code, setCode] = useState(null)

  const options = { lineNumbers: true, mode: 'javascript' }

  return CodeMirror && <CodeMirror
    onChange={code => setCode(code)}
    options={options}
    value={code}
  />
}

export default CodeEditor

from react-codemirror.

sslotsky avatar sslotsky commented on August 16, 2024 5

@Salakar Thanks that worked fantastically!

For what it's worth, require('react-codemirror') itself does not produce the error. Just including the modes does. So you could make that first require unconditional and render unconditionally as well.

from react-codemirror.

sslotsky avatar sslotsky commented on August 16, 2024 3

This was supposedly fixed but still happens as noted in #21

I would also like to use this package with SSR and currently cannot for this reason.

from react-codemirror.

ErwinSalas avatar ErwinSalas commented on August 16, 2024 1

I have a similar error, when i try to use react-player dependency, someone could helpme
ReferenceError: navigator is not defined
at FilePlayer.shouldUseHLS (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-player\lib\players\FilePlayer.js:173:41
)
at FilePlayer.render (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-player\lib\players\FilePlayer.js:268:25)
at resolve (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.development.js:2149:18)
at ReactDOMServerRenderer.render (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.dev
elopment.js:2260:22)
at ReactDOMServerRenderer.read (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.devel
opment.js:2234:19)
at renderToString (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.development.js:250
1:25)
at renderPage (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\server\render.js:174:26)
at Function.getInitialProps (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\server\document.js:83:25)
at _callee$ (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\lib\utils.js:36:30)
at tryCatch (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\regenerator-runtime\runtime.js:62:40

from react-codemirror.

saravanan-trika avatar saravanan-trika commented on August 16, 2024 1

react-codemirror2 this method working.

import { Controlled as CodeMirror } from "react-codemirror2";

let modeLoaded = false
if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
require('codemirror/mode/javascript/javascript')
modeLoaded = true
}
const Editor = (props) => {
const options = {
autoCloseBrackets: true,
lineWrapping: true,
mode: language,
}
if (modeLoaded) options.mode = 'javascript'

return (
<CodeMirror
onBeforeChange={handleChange}
onKeyDown = {handleKeyup}
value={value}
className={"demo"}
options={options}
/>
)
}

from react-codemirror.

wrannaman avatar wrannaman commented on August 16, 2024

Are you using server side rendered react? There won't be a navigator until the doc is ready.

from react-codemirror.

sslotsky avatar sslotsky commented on August 16, 2024

Seems it was removed here:

4624b3d#diff-2c23fe372e330148850d3aa80cedc390

from react-codemirror.

jflewis avatar jflewis commented on August 16, 2024

It seems the trick from above no longer works in NextJs. This is a terrible hack but the following works for now (if there is a better way in nextjs to do this please share).

In the imports/requires

let CodeMirror = null
try {
  navigator
  CodeMirror = require('react-codemirror')
  require('codemirror/mode/htmlmixed/htmlmixed')
  require('codemirror/mode/handlebars/handlebars')
} catch (error) {}

and in the render method

// render
   { CodeMirror && <CodeMirror ... /> }

from react-codemirror.

x5engine avatar x5engine commented on August 16, 2024

thank you :)

from react-codemirror.

prashantchothani avatar prashantchothani commented on August 16, 2024

@songyule Can you please help me with the full code where it is used please ?

from react-codemirror.

rohitninawe avatar rohitninawe commented on August 16, 2024

Thanks @Salakar and @sslotsky . works for me as well

from react-codemirror.

prashantchothani avatar prashantchothani commented on August 16, 2024

Thanks @sombreroEnPuntas It worked

from react-codemirror.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.