首页 > 解决方案 > 如何在Javascript中绑定一个函数

问题描述

我想在自身内部绑定函数,这样我就可以使用这个 setState 改变状态帮助我解决这个问题 function(response){ this.function=this.function.bind(this) console.log(response) }

标签: javascriptreactjsfunctionbind

解决方案


试试这个希望对你有用

axios.post(" http://${REACT_APP_SERVER_URL}/facebook", { uid: user }).then((response) => {
        console.log('hello')
        if (response.data.success) {
            const { checked } = this.state;
            const newChecked = [...checked];
            newChecked.push(1)
            this.setState({ checked: newChecked });
            console.log(this.state.checked)
            history.push("/dashboard");
        }
    }).catch(err=>{
        console.log(err)
    })

推荐阅读