我希望能够清楚地使用React refs来调用子函数.我有一个Parent组件,它是一个带有几个按钮的工具栏,在子组件中我可以访问库的导出功能.我想在父组件中单击按钮调用此导出功能.目前我正在使用React refs来完成这个:
Parent.js [参考]
class Parent extends React.Component {
onExportClick = () => {
this.childRef.export();
}
render() {
return (
<div>
<button onClick={this.onExportClick} />Export</button>
<Child ref={(node) => this.childRef = node;} />
</div>
)
}
}
Run Code Online (Sandbox Code Playgroud)
Child.js [参考]
class Child extends React.Component {
export() {
this.api.exportData();
}
render() {
<ExternalLibComponent
api={(api) => this.api = api}
/>
}
}
Run Code Online (Sandbox Code Playgroud)
该解决方案能正常工作,但我已经看到了很多的分歧.如果这是最好的做法.React 关于refs 的官方文档说我们应该"避免使用refs来做任何可以声明性地完成的事情".在一个类似问题的讨论帖中,React团队的Ben Alpert说"refs就是针对这个用例而设计的",但通常你应该尝试通过传递一个prop来声明性地做.
以下是我如何在没有声明的情况下执行此操作ref:
Parent.js [声明]
class Parent extends React.Component { …Run Code Online (Sandbox Code Playgroud) 我已经设法打开一个本地存储的ics文件,并且UIDocumentInteractionController将该文件识别为UTI com.apple.ical.ics(这是预期的)。数据显示了我所希望的大部分情况:

我对文档的印象是,UIDocumentInteractionController它将自动识别此ics文件类型并提供一些与日历相关的选项,但是,似乎没有任何方法可以从此视图将这些事件添加到日历中。在“打开方式”菜单下,我将邮件和保管箱视为选项,但未将日历作为选项。如果我通过电子邮件将其发送给自己,则可以从邮件应用中添加事件。我是否缺少这种观点的工作方式?Mobile Safari以类似的方式处理ics文件,除了在标题栏中有一个“全部添加”按钮。这正是我认为的行为方式。我现在不希望使用EventKit,因为我只是想模仿Safari处理文件的方式。
我们开发了一个React组件库供内部使用.该库使用webpack将所有内容捆绑到bundle.js中.该库作为NPM模块公开,供我们的应用程序使用,到目前为止,它已经运行良好.
最近我们添加了一个Grid组件,它具有一些非常大的外部依赖关系.尽管我们的一些应用程序不需要库中的这个组件,但我们决定将它包含在最终的包中.这可以使包大小产生巨大差异,因此我遵循Webpack的代码拆分指南,使用动态导入来打破Grid组件.对于将此库安装为NPM模块的应用程序,组件库包现在如下所示node_modules:
??? node_modules/
? ??? [our component library]/
? ? ??? Grid.js # Asynchronously loaded Grid component
? ? ??? bundle.js # Main bundle
? ? ??? ... other files in component library bundle
Run Code Online (Sandbox Code Playgroud)
在构建应用程序时(也使用Webpack),Grid组件似乎不会作为自己的文件包含在最终的bundle中.事实上,它根本没有包括在内:
??? dist/
? ??? main.js # App bundle
? ??? vendor.js # Vendor bundle created with CommonChunksPlugin
? ??? ... other files in application bundle but no Grid.js
Run Code Online (Sandbox Code Playgroud)
尝试在使用异步加载的Grid组件的浏览器中加载页面时,Webpack将返回以下错误:
Error: Loading chunk 0 failed.
at HTMLScriptElement.onScriptComplete (bundle.js:757)
Run Code Online (Sandbox Code Playgroud)
从本质上讲,这表示无法找到Grid组件块.我究竟做错了什么?甚至可以像这样编译拆分React组件库吗?
使用 Angular + Google Cloud Firestore,我希望调试从表单触发的请求,以确保它不会太频繁地触发。当我转到 Chrome 开发工具中的“网络”选项卡时,我看到一个请求通过https://firestore.googleapis.com/...,但响应数据已编码,如下所示:
8
[1,4,7]
Run Code Online (Sandbox Code Playgroud)
在我对 firestore 的非常基本的理解中,我认为请求看起来是这样的,因为 firestore 在幕后使用了 gRPC。我来自 REST/JSON 背景,所以这对我来说是一个新领域。有没有更好的方法来调试这样的请求?
一年多前,GoOffset向类型添加了一个值(请参阅此处json.UnmarshalTypeError已关闭的问题以了解上下文)。偏移值背后的目的是有道理的,但我不确定在读取类型为 的 go http 响应正文时如何使用它。io.ReadCloser
// An UnmarshalTypeError describes a JSON value that was
// not appropriate for a value of a specific Go type.
type UnmarshalTypeError struct {
Value string // description of JSON value - "bool", "array", "number -5"
Type reflect.Type // type of Go value it could not be assigned to
Offset int64 // error occurred after reading Offset bytes
}
Run Code Online (Sandbox Code Playgroud)
例如:
var body CustomType
decoderErr := json.NewDecoder(response.Body).Decode(&body)
if decoderErr != …Run Code Online (Sandbox Code Playgroud)