Javascript 可单击div中的模板文本中的链接不起作用(看起来我无法阻止页面重新加载)
所以我需要能够做到这一点,但是onClick函数不能被识别为一个函数,而是一个字符串 {e.stopPropagation;e.preventDefault history.push'/profile/test'}}>@test @test是${newmes} ```newText=testmessage.replace/@[a-z]+/i,`Javascript 可单击div中的模板文本中的链接不起作用(看起来我无法阻止页面重新加载),javascript,reactjs,Javascript,Reactjs,所以我需要能够做到这一点,但是onClick函数不能被识别为一个函数,而是一个字符串 {e.stopPropagation;e.preventDefault history.push'/profile/test'}}>@test @test是${newmes} ```newText=testmessage.replace/@[a-z]+/i,`{ e.stopPropagation(); e.preventDefault() history.push(\x27/profile/${user
{
e.stopPropagation();
e.preventDefault()
history.push(\x27/profile/${username}\x27)}>${newmes}</p></strong>`)
它有点工作,但是${newname}只显示一秒钟,然后消失,尽管我使用e.preventDefault来防止重新加载,对吗
编辑:
它现在可以工作了,我必须使用e.stopImmediatePropagation在组件的构造函数中添加以下行。如果这是state组件
this.handleclick = this.handleclick.bind(this)
onClick={() => handleClick()}
在同一onCLick中添加以下行,如果这是无状态组件
this.handleclick = this.handleclick.bind(this)
onClick={() => handleClick()}
试试上面的…它不是一个无状态组件,它是一个分配给innerHTMLonClick={=>handleClick}的字符串添加以上内容,它将工作不,它不会。innerHTML不支持JSX.Im使用reactFunctionalExportComponent。因此,我需要reactClassExportComponent才能将handleclick绑定到组件?不,这似乎对我不起作用