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

next build: window is not defined #811

Open
Pasoul opened this issue Aug 20, 2020 · 1 comment
Open

next build: window is not defined #811

Pasoul opened this issue Aug 20, 2020 · 1 comment

Comments

@Pasoul
Copy link

Pasoul commented Aug 20, 2020

创建BraftEditor.tsx子组件:

import BraftEditor, { EditorState } from 'braft-editor'
import NoSSR from 'components/NoSSR'
import React from 'react'

export default class Child extends React.Component {
  state = {
      // 创建一个空的editorState作为初始值
      editorState: BraftEditor.createEditorState(null)
  }

  async componentDidMount () {
      // 假设此处从服务端获取html格式的编辑器内容
      const htmlContent = "<div><b>123</b></div>"
      // 使用BraftEditor.createEditorState将html字符串转换为编辑器需要的editorStat
      this.setState({
          editorState: BraftEditor.createEditorState(htmlContent)
      })
  }

  submitContent = async () => {
      // 在编辑器获得焦点时按下ctrl+s会执行此方法
      // 编辑器内容提交到服务端之前,可直接调用editorState.toHTML()来获取HTML格式的内容
      const htmlContent = this.state.editorState.toHTML()
      console.log(htmlContent);
  }

  handleEditorChange = (editorState: EditorState) => {
      this.setState({ editorState })
  }

  render () {
      const { editorState } = this.state
      return (
          <div className="my-component">
            <BraftEditor
                    value={editorState}
                    onChange={this.handleEditorChange}
                    onSave={this.submitContent}
                />
          </div>
      )
  }
}

父组件引用:


import React from 'react'
import dynamic from 'next/dynamic'
import 'braft-editor/dist/index.css'

const BraftEditor = dynamic(() => import('./BraftEditor'), { ssr: false })

export default function EditorDemo() {
  return <BraftEditor />
}

开发模式下没问题可以正常使用富文本,但是执行yarn build报错:
ReferenceError: window is not defined
image

@mason-meng
Copy link

这个问题我解决了 https://blog.csdn.net/mengdasheng/article/details/113726981

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

2 participants