【发布时间】:2017-12-27 10:30:55
【问题描述】:
我正在使用 React Apollo 来查询我的数据存储中的所有记录,以便我可以在搜索过滤器中创建选择。
我使用的重要数据库模型是Report。
Report 具有 doorType、doorWidth、glass 和 manufacturer 字段。
当前,当查询响应时,我将 allReports 传递给多个通过数组并仅获取唯一项以创建可选列表的哑组件,就像这样..
const uniqueItems = []
items.map(i => {
const current = i[itemType]
if (typeof current === 'object') {
if (uniqueItems.filter(o => o.id !== current.id)) {
return uniqueItems.push(current)
}
} else if (!uniqueItems.includes(current)) {
return uniqueItems.push(current)
}
return
})
显然这段代码并不漂亮,而且有点矫枉过正。
当查询在我的SidebarFilter 组件中返回时,我想调度一个操作。这是查询...
const withData = graphql(REPORT_FILTER_QUERY, {
options: ({ isPublished }) => ({
variables: { isPublished }
})
})
const mapStateToProps = ({
reportFilter: { isPublished }
// filterOptions: { doorWidths }
}) => ({
isAssessment
// doorWidths
})
const mapDispatchToProps = dispatch =>
bindActionCreators(
{
resetFilter,
saveFilter,
setDoorWidths,
handleDoorWidthSelect
},
dispatch
)
export default compose(connect(mapStateToProps, mapDispatchToProps), withData)(
Filter
)
Redux 操作 setDoorWidths 基本上在 SidebarFilter 组件中执行上述代码,但它保存在存储中,因此如果用户返回页面,我不需要重新运行查询。
数据会更新并且侧边栏需要更改的情况非常罕见。
希望有一个使用props 参数到graphql 函数的解决方案。我觉得可以从ownProps 获取数据,然后可以在此处调度一个操作,但数据可能会出错或正在加载,这会破坏渲染。
编辑:
查询:
query ($isPublished: Boolean!){
allReports(filter:{
isPublished: $isPublished
}) {
id
oldId
dbrw
core
manufacturer {
id
name
}
doorWidth
doorType
glass
testBy
testDate
testId
isAssessment
file {
url
}
}
}
【问题讨论】:
标签: javascript reactjs redux graphql react-apollo