最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - React javascript check if string contains specific word and add something like link react routerstyles to specific wor

programmeradmin3浏览0评论

is there any solution how to check strings(ex:Google) and add Link react router into specific word(ex:Google)?

example like this photo

before

after

this is my very simple code

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

class App extends Component {
  state = {
    text: "Google is my Friend, Google is Search Engine, Thanks to google"
    website: "Google Friend"
  };

  render() {
    let text = null;
    if (this.state.text.includes(this.state.website)) {
      return (text = (
        <div>
          <Link to={"/example"}>Google </Link> then state text
        </div>
      ));
    }
    return <div>{text}</div>;
  }
}

export default App;

it would be more helpful if you can show me how to add style inside specific word(ex: Google) like changing color, or adding bold/italic or anything

thanks

is there any solution how to check strings(ex:Google) and add Link react router into specific word(ex:Google)?

example like this photo

before

after

this is my very simple code

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

class App extends Component {
  state = {
    text: "Google is my Friend, Google is Search Engine, Thanks to google"
    website: "Google Friend"
  };

  render() {
    let text = null;
    if (this.state.text.includes(this.state.website)) {
      return (text = (
        <div>
          <Link to={"/example"}>Google </Link> then state text
        </div>
      ));
    }
    return <div>{text}</div>;
  }
}

export default App;

it would be more helpful if you can show me how to add style inside specific word(ex: Google) like changing color, or adding bold/italic or anything

thanks

Share Improve this question edited Feb 14, 2020 at 11:21 Ste Fandy asked Feb 14, 2020 at 9:39 Ste FandySte Fandy 691 silver badge6 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

You can do it like this:

{this.state.text.split(" ").map(text => {
   return text === "Google" ? <Link to="/google">Google</Link> : text;
})}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论