- 高阶组件之高亮关键词
import React from "react";
const inp = "红包 返现 首充 权益"
let content = "参与活动,即有可能获得红包。首充还有机会获得更多权益哦!来一个红包吗?更有机会返现哦!"
const KeywordHoc = (ComponentView: any) => {
const DisplayName = (ComponentName: any) => {
return ComponentName.displayName || ComponentName.name || 'component'
}
const KeyRender: React.FC<{}> = () => {
let sliceKey = inp.split(' ')
for (let key in sliceKey) {
let regex = new RegExp(sliceKey[key], 'g')
console.log('regex', regex);
console.log(key);
content = content.replace(regex, `<span class="light">${sliceKey[key]}</span>`)
}
KeyRender.displayName = `Hoc${DisplayName(ComponentView)}`
let innerContent = <div dangerouslySetInnerHTML={{ __html: content }}></div>
return (
<ComponentView content={innerContent} />
)
}
return KeyRender
}
export default KeywordHoc
import React from "react";
import './index.css'
interface KeywodViewProps {
content: string
}
const KeywodView: React.FC<KeywodViewProps> = ({
content
}) => {
return (
<div>
<h1>KeywodView</h1>
{content}
</div>
)
}
export default KeywodView
import React from "react";
import KeywordHoc from 'package/component/KeywordHoc'
import KeywodView from 'package/component/KeywodView'
const Keyword : React.FC<{}> = (props) => {
const KeyTags = KeywordHoc(KeywodView)
return (
<div>
<h1>keyword</h1>
<KeyTags />
</div>
)
}
export default Keyword