繁体   English   中英

React-Redux-Firebase 新钩子

[英]React-Redux-Firebase new hooks

我已经设置了 redux 和 react,它们运行良好。 我现在想使用钩子 useFirestoreConnect 和 useFirestore() 将数据从 firestore 直接同步到 redux 状态。

此代码有效:

function BannerG() {

    const dispatch = useDispatch()
    // const banneAG = useSelector(state => state.banneAG)

    const firestore = useFirestore();
    useFirestoreConnect( props => [{collection : "layoutElements", doc: 'BannerG'}])
    const aa = useSelector(state  => state.firestore.data.layoutElements)
    console.log(aa)

在控制台打印对象 {BannerG: {H1: blah, H2:Blah blah, …………}}

但如果我改为使用

function BannerG() {

    const dispatch = useDispatch()
    // const banneAG = useSelector(state => state.banneAG)

    const firestore = useFirestore();
    useFirestoreConnect( props => [{collection : "layoutElements", doc: 'BannerG'}])
    const aa = useSelector(state  => state.firestore.data.layoutElements.BannerG)
    console.log(aa)

当我运行时,返回一个错误,指出 BannerG 未定义。 如果我尝试使用稍后的,aa.BannerG.H1 也会给出错误

任何人都有解决这个问题的想法?

我无法在任何参考文献中直接找到它,而只能在示例中找到它。 在此处输入图片说明

似乎 useSelector 需要方括号,如:

const aa = useSelector(state  => state.firestore.data.layoutElements['BannerG'])

我在示例github react-redux-firebasereact-reduxreact-redux-firebase.com 参考中发现它是这样的。

不幸的是,我现在无论如何都无法测试它,所以请让我知道它是否有效。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM