由于StatelessComponent不推荐使用,因此我尝试将所有组件转换为类。
我有一个界面,例如:
interface MyInterface{
prop1: number;
prop2: boolean;
}
Run Code Online (Sandbox Code Playgroud)
我在课堂上使用它:
class MyComponent extends React.Component<MyInterface> {
...
public render(){...}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试这样使用时MyComponent:
<MyComponent
prop1={3}
prop2={false}
/>
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
TS2740:类型{prop1:3,prop2:false}缺少ReadOnly类型的以下属性:render,context,setState,forceUpdate和另外3个属性。
有什么解决方法吗?
我正在开发一个使用 Firebase 的 Firestore 作为后端的 React Native 应用程序。现在,每次收到新消息时,我都会从 Firestore 中获取所有消息并更新我的状态,尽管这只是收到的一条新消息。
function listenCurrentChat(dispatch, chatID) {
const address = "chats/" + chatID + "/messages";
firebase.firestore().collection(address).orderBy('createdAt')
.onSnapshot(function (querySnapshot) {
dispatch({
type: CLEAR_MESSAGES,
payload: {
chatID: chatID,
}
});
querySnapshot.forEach(function (doc) {
//local
if (doc.metadata.hasPendingWrites) {
dispatch({
type: ADD_MESSAGE,
payload: {
chatID: chatID,
message: {...doc.data(), createdAt: new Date()}
}
});
} else {
dispatch({
type: ADD_MESSAGE,
payload: {
chatID: chatID,
message: {...doc.data(), createdAt: doc.data().createdAt.toDate()}
}
});
}
});
});
}
Run Code Online (Sandbox Code Playgroud)
显然,这非常糟糕,因为我只对一条新消息进行了多次读取,而且用户体验根本不好,因为存在持续的滞后。我还尝试将这些实时侦听器的地址保存在 redux 状态,并从最后一条消息的时间戳进行侦听,但这似乎也不是一个好的解决方案,因为我没有再考虑以前的消息的事实可以编辑。在 React …