V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
qwertty01
V2EX  ›  问与答

各位大佬, axios 异步请求,回调函数获取全局变量为空,怎么解决这个问题啊?

  •  1
     
  •   qwertty01 · 2020-09-23 20:54:04 +08:00 · 1214 次点击
    这是一个创建于 1304 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT,在线等

    第 1 条附言  ·  2020-09-23 21:51:27 +08:00
    export const Component= () => {
    let growl

    const onSubmitHandler = () => {
    axios.post(url, {
    }).then(res => {
    const.log(growl)
    })
    }
    return (
    <div>
    <Growl ref={(el) => growl = el} style={{marginTop: '75px'}}/>
    </div>
    )
    }
    5 条回复    2020-09-23 22:21:39 +08:00
    liberty1900
        1
    liberty1900  
       2020-09-23 21:00:01 +08:00 via Android
    Talk is cheap
    SilencerL
        2
    SilencerL  
       2020-09-23 21:15:02 +08:00 via iPhone
    Show me the fucking code.
    qwertty01
        3
    qwertty01  
    OP
       2020-09-23 21:52:02 +08:00
    @liberty1900 @SilencerL 麻烦大佬帮忙看一下
    Danswerme
        4
    Danswerme  
       2020-09-23 22:06:08 +08:00
    你这是要拿到 ref ? 函数式组件的话可以用 useRef
    import { useRef } from "react";
    ...

    let growl = useRef(null);

    文档见此 https://zh-hans.reactjs.org/docs/hooks-reference.html#useref

    另外你 then 里面的 const.log 是什么玩意?
    qwertty01
        5
    qwertty01  
    OP
       2020-09-23 22:21:39 +08:00
    @Danswerme 多谢大佬指点,ref 这块一直没太学明白。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2869 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 91ms · UTC 13:42 · PVG 21:42 · LAX 06:42 · JFK 09:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.