小编Арт*_*тов的帖子

如何使用 thunk 在 react-redux 钩子中进行异步调用?

我开始学习钩子。但我不明白如何正确使用异步调用。早些时候我用过

import * as actionQR from "../actions/qr";
...
function mapDispatchToProps(dispatch) {
    return {
        actionQR: bindActionCreators(actionQR, dispatch),
    }
} 
Run Code Online (Sandbox Code Playgroud)

在调用 my 之后this.props.actionQR.myFunc(),我应该用 useDispatch() 做什么?如果我只是打电话

import {foo} from "../actions/qr";
...
useDispatch(foo());
Run Code Online (Sandbox Code Playgroud)

那么我foo()console.log(2)

export const foo = () => {
    console.log(1);
    return (dispatch) => {
        console.log(2);
      }
}
Run Code Online (Sandbox Code Playgroud)

我使用 thunk

import createRootReducer from './reducers/index';
...
const store = createStore(createRootReducer, applyMiddleware(thunk));
Run Code Online (Sandbox Code Playgroud)

reactjs redux redux-thunk react-redux react-hooks

6
推荐指数
1
解决办法
6441
查看次数

firebase 实时数据库一次 vs 开启?

我在 Node js 上使用 firebase 实时数据库,就像 API 数据库一样。

once()和之间有什么不同on()

我的代码once()运行速度非常慢。

需要它做什么off()

例子

router.get('/:qrid', async(req, res)=>{
    let id = req.params.qrid;
    let ref = firebase.database().ref('/qr/'+id);
    let snapshot = await ref.once('value');
    res.json(Object.assign({}, snapshot.val()));
});
Run Code Online (Sandbox Code Playgroud)

这个工作非常慢(250ms-3000ms)。当我使用 on() 时,速度更快。

router.get('/:qrid',(req, res)=>{
    let id = req.params.qrid;
    let ref = firebase.database().ref('/qr/'+id);
    ref.on('value',(snapshot) => res.json(Object.assign({}, snapshot.val())));
});
Run Code Online (Sandbox Code Playgroud)

database node.js firebase firebase-realtime-database

4
推荐指数
1
解决办法
3652
查看次数

如何在 Web 登录后获取用户 ID 令牌

我对 node.js 使用 firebase-admin,对 React 使用 firebase。我想获取访问令牌以将其发送到标头中的服务器以保护我的数据。我在反应应用程序上使用此代码

firebase.auth().signInWithEmailAndPassword(form.elements.email.value, form.elements.password.value)
                .then((result) => {
                        console.log(result)
                })
                .catch(function(error) {
                    var errorCode = error.code;
                    var errorMessage = error.message;
                    console.log(errorMessage);
                });
Run Code Online (Sandbox Code Playgroud)

但我不知道如何从结果中获取令牌。或者我有一些方法可以在我的服务器上使用 API?它应该像这样工作:如果用户想要进入管理面板,他应该登录。客户端获取令牌,在 cookie 中设置并用于任何请求?

javascript firebase firebase-authentication

4
推荐指数
1
解决办法
3236
查看次数