我正在构建一个React应用程序,需要使用外部API来获取一些文本.问题是文本以带有HTML标签的字符串形式返回以显示重点,例如:
{ "text": ["The quick <em class='hl'>brown</em> fox"] }
Run Code Online (Sandbox Code Playgroud)
我一直在使用dangerouslySetInnerHTML像这样
{this.props.text.map(function(snippet) {
return <div dangerouslySetInnerHTML={{__html: snippet }}></div>
})}
Run Code Online (Sandbox Code Playgroud)
大部分时间都在工作,但是我遇到了这样的实例,其中API返回的文本格式化为React抛出以下错误:
Uncaught (in promise) Error: Invariant Violation:
Danger: Expected markup to render 7 nodes, but rendered 4
Run Code Online (Sandbox Code Playgroud)
似乎目前无法捕获渲染函数中的错误,所以我不能忽略有问题的并继续.
是一种更好的方法来处理文本强调或使用React突出显示,或者可能有一种方法来过滤或捕获React可能会发现问题的字符串?
在浏览器中使用 Mapbox GL JS 时,包括在 Chrome 中使用模拟模式,该事件contextmenu响应地图上的长按/点击。但是,在真实设备上,当用户点击并按住地图时,不会触发此事件。
在 Mapbox GL JS 中聆听地图上长按的最佳方式是什么?
导出具有其他依赖关系的模块时,最好是在模块导出函数中还是在模块导出函数之外包含该依赖关系?我通常会看到后者,但似乎最好将它保留在本地范围内.
例如:
var foo = require('foo');
module.exports = function(d) {
return foo(d)/2;
}
Run Code Online (Sandbox Code Playgroud)
与
module.exports = function(d) {
var foo = require('foo');
return foo(d)/2;
}
Run Code Online (Sandbox Code Playgroud) 我知道您可以将 Express 配置为输出漂亮的 JSON ( app.set("json spaces", 2)) 或缩小的 JSON ( app.set("json spaces", 0)),但是有没有办法在特定响应上覆盖此全局设置?
例如,如果我设置json spaces为0,我可以很好地打印以下内容:
app.get("/foo", function(req,res) {
res.json({"a":"b"}, 2);
});
Run Code Online (Sandbox Code Playgroud)
谢谢!