简体   繁体   English

更改 svg 颜色:React.createElement:类型无效 — 应为字符串

[英]Change svg color : React.createElement: type is invalid — expected a string

I have a section I would like on click to change the color of SVG color,我有一个部分我想点击更改 SVG 颜色的颜色,

Here is my solution so far到目前为止,这是我的解决方案

    import { ReactComponent as DownloadSVG } from 'assets/images/share_download.svg';
        .......
    
    const Demo = () => {
    
        return (
         <div className={`download-options ${tab ==='downloadoptions' && 'active-tab'}`}>
           <span style={{ backgroundColor: isBlack ? '#262626' : '#F3F3F3'}} className="download_icon" onClick={handleDownloadTabClick}>
                        <DownloadSVG style={{ fill: isBlack ? '#fff' : '#262626'}} />
            </span>
          <span className="download_title media-text">DOWNLOAD</span>
         </div>
    )
 }
export default Demo;

Unfortunatelly iam getting the following error不幸的是,我收到以下错误

React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports

What is wrong here?这里有什么问题?

I believe it's the tab variable declaration.相信这是tab变量声明。

Try:尝试:

import { ReactComponent as DownloadSVG } from "assets/images/share_download.svg";

const Demo = () => {
  return (
    <div
      className={`download-options ${
        tab && tab === "downloadoptions" ? "active-tab" : ""
      }`}
    >
      <span
        style={{ backgroundColor: isBlack ? "#262626" : "#F3F3F3" }}
        className="download_icon"
        onClick={handleDownloadTabClick}
      >
        <DownloadSVG style={{ fill: isBlack ? "#fff" : "#262626" }} />
      </span>
      <span className="download_title media-text">DOWNLOAD</span>
    </div>
  );
};

export default Demo;

Why this works为什么这有效

It's very difficult to really know because I can only see part of the source code – so, my best guess is that on initial render, the tab variable is not defined (yet), and therefore is undefined .很难真正知道,因为我只能看到部分源代码——所以,我最好的猜测是在初始渲染时, tab变量没有定义(还),因此是undefined

When you use short-hand logic like this: tab === "downloadoptions" && 'active-tab' , JavaScript returns the value false when the part before the && is falsey.当您使用这样的简写逻辑时: tab === "downloadoptions" && 'active-tab' ,当&&之前的部分为 false 时,JavaScript 返回false This is a Boolean value, where React is expecting a String .这是一个Boolean值,其中 React 需要一个String

You can see it when you paste this into your browser console:将其粘贴到浏览器控制台时可以看到它:

let tab; // this sets the variable, but it is still "undefined"
console.info(tab === "downloadoptions" && 'active-tab'); // returns: false

It is therefore better to use a Ternary operator that will always return a string.因此,最好使用始终返回字符串的三元运算符

Like so:像这样:

let tab; // this sets the variable, but it is still "undefined"
console.info(tab && tab === "downloadoptions" ? 'active-tab' : 'nothing'); // returns: "nothing"

Hope this clears things up.希望这可以解决问题。

暂无
暂无

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

相关问题 警告:React.createElement:类型无效——需要一个字符串 - Warning: React.createElement: type is invalid — expected a string React.createElement: type is invalid -- 期望一个字符串或 function - React.createElement: type is invalid --expected a string or function React.createElement:类型无效-预期为字符串-index.js - React.createElement: type is invalid — expected a string — index.js ReactJS-错误消息:React.createElement:类型无效-预期为字符串 - ReactJS - Error Message: React.createElement: type is invalid — expected a string react-hot-loader:React.createElement:type无效 - 预计更新屏幕时会出现字符串错误 - react-hot-loader: React.createElement: type is invalid — expected a string error updating the screen React 和 Typescript,警告:React.createElement:类型无效——需要一个字符串但得到:未定义 - React and Typescript, Warning: React.createElement: type is invalid -- expected a string but got: undefined React Native导航包错误React.createElement:类型无效-预期为字符串 - React Native navigation package Error React.createElement: type is invalid — expected a string 警告:React.createElement:类型无效-预期为字符串(对于内置组件) - Warning: React.createElement: type is invalid — expected a string (for built-in components) 没有拼写错误,但不断获取React.createElement:类型无效-预期为字符串 - Got No Spelling Error but keep getting React.createElement: type is invalid — expected a string React.createElement: 类型无效——需要一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:对象 - React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM