简体   繁体   English

当我记录 mixCategory 是 firstName[object Object]lastName,如何在字符串中插入图标

[英]When I log mixedCategory is firstName[object Object]lastName, How to insert icon inside a string

state = { icon: , }

loadAllCategories = async () => 
{ 
  const{icon} = this.state; 
  firstCategory ="Electronic Devices";
  lastCategory = "Phone"; mixedCategory =firstCategory+ icon + lastCategory;
};

You need to return as a jsx value as shown below, for wrapping you can use React.Fragments您需要作为 jsx 值返回,如下所示,对于包装,您可以使用React.Fragments

let mixedCategory = (
  <>
    {firstCategory} {icon} {lastCategory}
  </>
);

Code代码

import React, { Component } from "react";
import ReactDOM from "react-dom";

const Icon = () => {
  return (
    <svg height="100" width="100">
      <circle
        cx="50"
        cy="50"
        r="40"
        stroke="black"
        stroke-width="3"
        fill="red"
      />
      Sorry, your browser does not support inline SVG.
    </svg>
  );
};

class App extends Component {
  state = { icon: <Icon />, mixedValue: null };

  loadAllCategories = () => {
    const { icon } = this.state;
    let firstCategory = "Electronic Devices";
    let lastCategory = "Phone";
    let mixedCategory = (
      <>
        {firstCategory} {icon} {lastCategory}
      </>
    );
    this.setState({
      mixedValue: mixedCategory
    });
  };

  render() {
    return (
      <>
        <button onClick={this.loadAllCategories}>Click here</button>
        {this.state.mixedValue && this.state.mixedValue}
      </>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

see the working example in the codepen请参阅codepen 中的工作示例

@Update @更新

as mentioned in the comments using FontAwesomIcon如使用 FontAwesomIcon 的评论中所述

import React, { Component } from "react";
import ReactDOM from "react-dom";
import { library } from "@fortawesome/fontawesome-svg-core";
import { fab } from "@fortawesome/free-brands-svg-icons";
import { faInfo, faCoffee } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";

library.add(fab, faInfo, faCoffee);

class App extends Component {
  state = { icon: <FontAwesomeIcon icon="info" />, mixedValue: null };

  loadAllCategories = () => {
    const { icon } = this.state;
    let firstCategory = "Electronic Devices";
    let lastCategory = "Phone";
    let mixedCategory = (
      <>
        {firstCategory} {icon} {lastCategory}
      </>
    );
    this.setState({
      mixedValue: mixedCategory
    });
  };

  render() {
    return (
      <>
        <button onClick={this.loadAllCategories}>Click here</button>
        {this.state.mixedValue && this.state.mixedValue}
      </>
    );
  }
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

codepen 代码笔

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

相关问题 我如何在反应中将名字/姓氏更改为名字/姓氏? 问题是它来自后端 - how can i change firstname/lastname to firstName/lastName in react? the problem is that it comes from the backend 如何控制台对象内的日志对象 - How to console log object inside of an object 如何在 mui 数据表的同一列中显示名字和姓氏? - how do i display firstname and lastname in same column in mui data tables? 如何显示元素名字和姓氏...来自 strore redux - how to display element firstname and lastname ... from strore redux 如何将字符串添加到对象内的字符串值? - How can I add a string to a string value inside an object? 如何正确警告 reactjs 中的某些变量姓氏和名字 - How to properly alert certain variable lastname and firstname in reactjs 我可以部分显示我的内容(显示名字而不是名字和姓氏) - I can partially display my content (displaying firstName instead of firstName and lastName) 当我尝试使用 svg 图标时反应返回 [object Object] - React return [object Object] when i try use svg icon 当原始字符串是姓氏,名字时,如何在文本字段中显示名字? 反应/Spfx - How to display firstname in a textfield when original string is surname, firstname? React/Spfx 如何将跨度元素插入 object(字符串)? - How to insert a span element into an object (string)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM