简体   繁体   English

如何将“const”合并到另一个“const”中,因为我无法在 React 中修改“export”

[英]How to merge a “const” into another “const since I can not modify ”export" in React

I want to include Theme UI 's presets in my GatsbyJS for switching the mode (dark and light) and the themes.我想在我的 GatsbyJS 中包含Theme UI预设,用于切换模式(明暗)和主题。

I can't modify export of my original code to merge two const functions into an export , because export needs children to be defined.我无法修改原始代码的export以将两个const函数合并到一个export中,因为export需要定义children级。 I will not want to use children , because another layout file in JSX has already children .我不想使用children ,因为 JSX 中的另一个布局文件已经有了children I also can't merge a const into another const .我也无法将const合并到另一个const中。 You will check the errors at the end.您将在最后检查错误。 I'll share my repo's small folder at the end for you to test.最后我会分享我的 repo 的小文件夹供你测试。 The In Angular2+ is there any way to extends on inherit const into another const? 在 Angular2+ 中,有没有办法将继承 const 扩展到另一个 const? does not help either.也无济于事。

See my small original code, and notice const and export :查看我的小原始代码,并注意constexport

import React from 'react'
import {Context} from '../../common'
import SelectLanguage from './SelectLanguage'
import {Links} from './styles'
import {Navegador, StyledHeader} from './styles'

const Header = () => (
  <Context.Consumer>
    {({ toggleLanguage, lang }) => (
      <StyledHeader>
        <Navegador id="menu">
          <li>
            <Links>
              {/* ... */}
              <SelectLanguage lang={lang} toggleLanguage={toggleLanguage} />
            </Links>
          </li>
        </Navegador>
      </StyledHeader>
    )}
  </Context.Consumer>
)

export default Header

My another small Theme UI code, you can notice const functions inside the export :我的另一个小的 Theme UI 代码,您可以注意到export中的const函数:

/** @jsx jsx */

import { jsx, Styled, useColorMode } from 'theme-ui'
import ButtonUI from '../../theme-ui/button-ui'

const themes = ['deep', 'funk', 'future', 'swiss']
const modes = ['default', 'dark']

const getThemeName = (theme) => 
{
  switch (theme)
  {
    case 'deep':
      return 'Deep'
    case 'funk':
      return 'Funk'
    case 'future':
      return 'Future'
    case 'swiss':
      return 'Swiss'
    default:
        return theme
  }
}

const getModeName = (mode) => 
{
  switch (mode)
  {
    case 'dark':
      return (<span role="img" aria-label="moon">🌕 </span>)
    case 'default':
      return (<span role="img" aria-label="sun">☀️ </span>)
    default:
      return mode
  }
}

export default function Layout({ children }) 
{

  const [theme, setTheme] = useColorMode()
  const [mode, setMode] = useColorMode()

  const cycleTheme = (e) => 
  {
    const i = themes.indexOf(theme)
    const next = themes[(i + 1) % themes.length]
    setTheme(next)
  }

  const cycleMode = (e) => 
  {
    const i = modes.indexOf(mode)
    const next = modes[(i + 1) % modes.length]
    setMode(next)
  }

  return (
    <Styled.root>
          <ButtonUI
            sx={{
              ml: 2,
            }}
            onClick={cycleTheme}>
            <span role="img" aria-label="theme">🎨 </span>
            {getThemeName(theme)}
          </ButtonUI>
          <ButtonUI
            sx={{
              ml: 2,
            }}
            onClick={cycleMode}>
            {getModeName(mode)}
          </ButtonUI>
    </Styled.root>
  )
}

Let's merge the second code into the first code, and I renamed const to function , since I can not put const into another const or modify export (because children needs to be defined):让我们将第二个代码合并到第一个代码中,我将const重命名为function ,因为我不能将const放入另一个const或修改export (因为需要定义children ):

/** @jsx jsx */

import { jsx, Styled, useColorMode } from 'theme-ui'
import {Context} from '../../common'
import SelectLanguage from './SelectLanguage'
import {Links} from './styles'
import {Navegador, StyledHeader} from './styles'
import ButtonUI from '../../theme-ui/button-ui'

const themes = ['deep', 'funk', 'future', 'swiss']
const modes = ['default', 'dark']

const getThemeName = (theme) => 
{
  switch (theme)
  {
    case 'deep':
      return 'Deep'
    case 'funk':
      return 'Funk'
    case 'future':
      return 'Future'
    case 'swiss':
      return 'Swiss'
    default:
        return theme
  }
}

const getModeName = (mode) => 
{
  switch (mode)
  {
    case 'dark':
      return (<span role="img" aria-label="moon">🌕 </span>)
    case 'default':
      return (<span role="img" aria-label="sun">☀️ </span>)
    default:
      return mode
  }
}

const [theme, setTheme] = useColorMode();
const [mode, setMode] = useColorMode();

function cycleTheme (e) {
  const i = themes.indexOf(theme)
  const next = themes[(i + 1) % themes.length]
  setTheme(next)
}

function cycleMode (e) {
  const i = modes.indexOf(mode)
  const next = modes[(i + 1) % modes.length]
  setMode(next)
}

function Header () {
  return (

    <Styled.root>
      <Context.Consumer>
        {({ toggleLanguage, lang }) => (
          <StyledHeader>
            <Navegador id="menu">
              <li>
                <ButtonUI
                  sx={{
                    ml: 2,
                  }}
                  onClick={cycleTheme}>
                  <span role="img" aria-label="theme">🎨 </span>
                  {getThemeName(theme)}
                </ButtonUI>
                <ButtonUI
                  sx={{
                    ml: 2,
                  }}
                  onClick={cycleMode}>
                  {getModeName(mode)}
                </ButtonUI>
                <Links>
                  <SelectLanguage lang={lang} toggleLanguage={toggleLanguage} />
                </Links>
              </li>
            </Navegador>
          </StyledHeader>
        )}
      </Context.Consumer>
    </Styled.root>
  )
}

export default Header

The errors appeared:出现的错误:

Error: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/warnings/invalid-hook-call-warning.html for tips about how to debug and fix this problem.
resolveDispatcher
node_modules/react/cjs/react.development.js:1465
useContext
node_modules/react/cjs/react.development.js:1473
patch/React$$1.useContext
node_modules/react-hot-loader/dist/react-hot-loader.development.js:2939
useThemeUI
node_modules/@theme-ui/core/dist/index.esm.js:40
useColorMode
node_modules/@theme-ui/color-modes/dist/index.esm.js:169
./src/components/theme/Header/index.jsx/<
src/components/theme/Header/index.jsx:43

  40 |   }
  41 | }
  42 | 
> 43 | const [theme, setTheme] = useColorMode();
  44 | const [mode, setMode] = useColorMode();
  45 | 
  46 | function cycleTheme (e) {

./src/components/theme/Header/index.jsx

You can download small folder of my repo: http://sendanywhe.re/HQ19EMJK or https://filetransfer.io/data-package/i03Or3CD or https://fromsmash.com/Gatsby-i81n-Starter . You can download small folder of my repo: http://sendanywhe.re/HQ19EMJK or https://filetransfer.io/data-package/i03Or3CD or https://fromsmash.com/Gatsby-i81n-Starter . Use index-example.js .使用index-example.js

You could put all of the Layout functionality into your Header component, but why are doing all of this?您可以将所有布局功能放入您的 Header 组件中,但为什么要这样做呢?

// Above this is your imports and various constants
function Header () {
  const [theme, setTheme] = useColorMode();
  const [mode, setMode] = useColorMode();

  function cycleTheme (e) {
    const i = themes.indexOf(theme)
    const next = themes[(i + 1) % themes.length]
    setTheme(next)
  }

  function cycleMode (e) {
    const i = modes.indexOf(mode)
    const next = modes[(i + 1) % modes.length]
    setMode(next)
  }
  
  return (

    <Styled.root>
      <Context.Consumer>
        {({ toggleLanguage, lang }) => (
          <StyledHeader>
            <Navegador id="menu">
              <li>
                <ButtonUI
                  sx={{
                    ml: 2,
                  }}
                  onClick={cycleTheme}>
                  <span role="img" aria-label="theme">🎨 </span>
                  {getThemeName(theme)}
                </ButtonUI>
                <ButtonUI
                  sx={{
                    ml: 2,
                  }}
                  onClick={cycleMode}>
                  {getModeName(mode)}
                </ButtonUI>
                <Links>
                  <SelectLanguage lang={lang} toggleLanguage={toggleLanguage} />
                </Links>
              </li>
            </Navegador>
          </StyledHeader>
        )}
      </Context.Consumer>
    </Styled.root>
  )
}

export default Header;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM