在React中使用onclick - 需要定义吗?

时间:2018-03-22 08:11:45

标签: reactjs

如何让这个onclick函数在ReactJS中运行?

const Message = (props) => (
    <a onClick={kayako.maximize()} href="javascript:;">
        Button to "kayako.maximize()" chat
    </a>
)
export default Message

实时聊天的脚本是

<script>{'(function(d,a){function c(){var b=d.createElement("script");b.async=!0;b.type="text/javascript";b.src=a._settings.messengerUrl;b.crossOrigin="anonymous";var c=d.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c)}window.kayako=a;a.readyQueue=[];a.newEmbedCode=!0;a.ready=function(b){a.readyQueue.push(b)};a._settings={apiUrl:"https://bold.kayako.com/api/v1",teamName:"Bold Support",homeTitles:[{"locale":"en-us","translation":"Hello! "},{"locale":"id","translation":""}],homeSubtitles:[{"locale":"en-us","translation":"Welcome to Bold Support. Let's chat — start a new conversation below."},{"locale":"id","translation":""}],messengerUrl:"https://bold.kayakocdn.com/messenger",realtimeUrl:"wss://kre.kayako.net/socket",widgets:{presence:{enabled:true},twitter:{enabled:false,twitterHandle:"3299444720"},articles:{enabled:false,sectionId:11}},styles:{primaryColor:"#F1703F",homeBackground:"-192deg, #40364D 37%, #9B4779 100%",homePattern:"https://assets.kayako.com/messenger/pattern-1--dark.svg",homeTextColor:"#FFFFFF"}};window.attachEvent?window.attachEvent("onload",c):window.addEventListener("load",c,!1)})(document,window.kayako||{});'}</script>

但是如何定义Kayako以便聊天窗口打开?以下是Kayako - Example

给出的示例

1 个答案:

答案 0 :(得分:1)

kayako似乎是一个外部全局变量。 注意react事件处理程序属性接受一个函数。在您的代码中,您立即评估maximize并传递结果,而不是将函数传递给onClick。 尝试:

<a onClick={() => kayako.maximize()}>