【发布时间】:2019-09-11 16:00:12
【问题描述】:
在我的一个组件上,我有一个外部小部件,它需要一个脚本标签和一个特殊的 div 元素。页面加载后,脚本会在 div 中插入 iframe。
我遇到的问题是该小部件仅在页面像普通 http 页面一样加载时才起作用。当我使用锚标签时,这个小部件可以工作,但是使用 Link 时它不会因为没有页面加载。
列表组件----Link---->属性组件
如何使用 Link 实现相同的行为?
App.js
<Switch>
<Route path="/" exact component={Home} />
<Route path="/list" exact component={List} />
<Route path="/property/:id/:sleeps/:start/:end" exact component={Property} />
<Route component={Error} />
</Switch>
List.js
<Link to={`/property/${property.id}/${property.sleeps}/${property.start}/${property.end}`}>
<img src={property.image} alt={property.name}/>
</Link>
Property.js
componentDidMount = () => {
//function inserts script to body
this.loadScript('https://widgetSite.co.uk/components/embed.js');
}
//inside render
<div
data-calendar-key="widget key"
data-calendar-property-id={this.propID}
id="calendar-js">
Your widget will appear here.
</div>
【问题讨论】:
-
你尝试过 React 门户吗?您可以使用门户呈现 DOM 层次结构之外的子级。
-
@Ray 不,我没听说过,现在看看
标签: javascript reactjs react-router