我有一个包含几层子组件的表单。表单的状态保持在最高级别,而我向下传递作为更新顶层的道具的函数。唯一的问题是,当表单变得很大(您可以动态添加问题)时,每个单个组件都会在其中一个更新时重新加载。这是我的代码的简化版本(或代码框:https : //codesandbox.io/s/636xwz3rr ):
const App = () => {
return <Form />;
}
const initialForm = {
id: 1,
sections: [
{
ordinal: 1,
name: "Section Number One",
questions: [
{ ordinal: 1, text: "Who?", response: "" },
{ ordinal: 2, text: "What?", response: "" },
{ ordinal: 3, text: "Where?", response: "" }
]
},
{
ordinal: 2,
name: "Numero dos",
questions: [
{ ordinal: 1, text: "Who?", response: "" },
{ ordinal: 2, text: "What?", response: …Run Code Online (Sandbox Code Playgroud) 我已经搜索了互联网和 StackOverflow,但我找不到答案,甚至找不到问题。
我有两个集合,reports和users. 我希望我的查询返回所有报告并指示指定用户是否将该报告作为其数组中的收藏夹。
报告收集
{ _id: 1, name:"Report One"}
{ _id: 2, name:"Report Two"}
{ _id: 3, name:"Report Three"}
Run Code Online (Sandbox Code Playgroud)
用户收藏
{_id: 1, name:"Mike", favorites: [1,3]}
{_id: 2, name:"Tim", favorites: [2,3]}
Run Code Online (Sandbox Code Playgroud)
users.name="Mike" 的期望结果
{ _id: 1, name:"Report One", favorite: true}
{ _id: 2, name:"Report Two", favorite: false}
{ _id: 3, name:"Report Three", favorite: true}
Run Code Online (Sandbox Code Playgroud)
我能找到的所有答案都在 local ( reports) 字段上使用 $unwind ,但在这种情况下, local 字段不是数组。外部字段是数组。
怎样才能放松异域呢?有一个更好的方法吗?
我在网上看到有人建议制作另一个favorites包含以下内容的集合:
{ _id: 1, userId: 1, reportId: 1 } …Run Code Online (Sandbox Code Playgroud)