简体   繁体   English

从dragonallySetInnerHtml内容调用React类函数

[英]Call React class function from dangerouslySetInnerHtml content

For an application I am developing using ReactJS, I have to make an ajax call out to a service which is either going to return a JSON object (if successful) or a small amount of HTML code (if not successful) . 对于使用ReactJS开发的应用程序,我必须对服务进行ajax调用,该服务要么返回JSON对象(如果成功),要么返回少量HTML代码(如果不成功) This HTML code has a link inside of it which needs to launch a modal control in my react app. 该HTML代码内部具有一个链接,该链接需要在我的react应用中启动模式控件。 I cannot alter the HTML coming back from the ajax call because it is used in other applications. 我无法更改从ajax调用返回的HTML,因为它已在其他应用程序中使用。

The HTML will look something like this: HTML将如下所示:

<span>ERROR MESSAGE HERE.</span>
<span>
    <a data-error-code="141096">
        <u>See details.</u>
    </a>
</span>

When the user clicks on that "See details" link, the modal should open, go get the detail message from the server and display it in the modal. 当用户单击该“查看详细信息”链接时,应该打开模态,从服务器获取详细信息并在模态中显示它。 This works on other areas of the site already. 这已经可以在网站的其他区域使用了。

What I'm struggling with is handling the onClick() event attached to the <a> tag. 我正在努力处理的是附加到<a>标记的onClick()事件。 I have a method on my react component that the HTML will be rendered in named showErrorModal which will open the error detail modal. 我的react组件上有一个方法,该HTML将以名为showErrorModal方式呈现,这将打开错误详细信息模式。

Any help is greatly appreciated! 任何帮助是极大的赞赏!


UPDATE: A jsFiddle with sample of what I need can be found here . 更新:可以在这里找到带有我需要的示例的jsFiddle。 In this case, I've hard-coded the HTML that I'm injecting into the div area. 在这种情况下,我已经对要注入div区域的HTML进行了硬编码。 What I need to do is to add an onClick() event to that anchor tag in the htmlToInject HTML string to call handleClick() . 我需要做的是向htmlToInject HTML字符串中的锚标记添加一个onClick()事件,以调用handleClick() I can't modify the contents of htmlToInject since that is generated server-side and I have no control over that. 我无法修改htmlToInject的内容,因为它是在服务器端生成的,对此我无法控制。 This is pretty much the exact structure the server will be returning, and the reason I need that error messaging is because it is dynamic based on what the error is (ex: "There are no items in your bag that this coupon applies to" vs "This coupon is expired" vs "You don't qualify to use this coupon"). 这几乎是服务器将要返回的确切结构,我之所以需要该错误消息传递是因为它基于错误的原因是动态的(例如:“袋中没有此优惠券适用的物品”与“此优惠券已过期”与“您没有资格使用此优惠券”)。 The separate messages are returned in individual <span> tags. 单独的<span>标签返回单独的消息。 In the server call I do also get the error code returned so I don't need to worry about reading the data-error-code attribute. 在服务器调用中,我也会得到返回的错误代码,因此我不必担心读取data-error-code属性。 what I get back from the server looks like: 我从服务器获得的信息如下:

 { success: false, error-code: 123456, error: "some HTML goes here" } 

When the call returns with data, I do a this.setState() to set those 3 values, so I already know what the error code is. 当调用返回数据时,我执行this.setState()来设置这3个值,所以我已经知道错误代码是什么了。

I used some jQuery applied in just the right place ( updated jsFiddle ): 我在正确的地方使用了一些jQuery( 更新了jsFiddle ):

componentDidMount: function() {
    $('.outsideHtml a').click(function(e){
        alert($(e.target).attr('data-error-code'))
    })
}

You'll probably want componentDidUpdate instead of or as well as componentDidMount , if the component has an initial state before rendering the HTML (such as a 'Loading...' state). 你可能想componentDidUpdate代替或以及componentDidMount ,如果组件渲染HTML前的初始状态(如“正在加载...”状态)。 You may also need to do some this wrangling in the click handler function (or use ES2015) to call methods on the component instance. 您可能还需要在单击处理程序函数中进行一些this (或使用ES2015)以在组件实例上调用方法。

I found that jQuery's on method didn't catch DOM inserted by React (at least via dangerouslySetInnerHTML ). 我发现jQuery的on方法没有捕获React插入的DOM(至少通过dangerouslySetInnerHTML )。 I'm guessing React's DOM manipulation bypasses something jQuery relies on. 我猜想React的DOM操作会绕过jQuery依赖的东西。 By the time the componentDid... functions are called the DOM is ready for jQuery. 等到componentDid...函数被调用时,DOM就可以使用jQuery。

Adding the click event handler could probably be achieved relatively easily in the DOM without jQuery but the concept would be the same. 在没有jQuery的DOM中,可以很容易地添加click事件处理程序,但是概念是相同的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM