javascript - React中如何在函数中调用函数(auth、路由)

标签 javascript reactjs function authentication routes

我正在尝试创建一个在加载 DOM 时直接执行的组件 onInit();

此函数将 token 发布到端点,如果成功,我将尝试运行一个名为“valid()”的函数

我不断遇到的问题是,当我尝试调用“有效”函数作为响应时,它说不能历史未定义。

我认为我没有以正确的方式传递 Prop 。

此外,如果不成功,应返回错误页面。

感谢您对此的任何帮助

export class LandingPage extends Component {
    constructor(props) {
        super(props);
        this.state = {};
        this.valid = this.valid.bind(this);
    }

    valid = () => {
        auth.login(() => {
            this.props.history.push("/app");
        });
    };

    componentDidMount() {
        onInit();

        function onInit(props) {

            const apiUrl = "www.somefakedomain.com/endpoint"

            axios
                .post(apiUrl, {
                    token: 'somevalue123'
                })
                .then(function(response) {
                    console.log(response);
                    //CALL VALID FUNCTION HERE
                    this.valid; //throws error, how to run function here
                })
                .catch(function(error) {
                    console.log(error);
                    //Show Error Page
                });
        }
    }
    render() {
        return (
            <div>
                <Spinner />
            </div>
        );
    }
}

最佳答案

您没有向 onInIt 函数传递任何内容。

您可能正在尝试做这样的事情吗? -

export class LandingPage extends Component {
    constructor(props) {
        super(props);
        this.state = {};
        this.valid = this.valid.bind(this);
    }

    valid = () => {
        auth.login(() => {
            this.props.history.push("/app");
        });
    };

    componentDidMount() {
        function onInit(props) {

            const apiUrl = "www.somefakedomain.com/endpoint"

            axios
                .post(apiUrl, {
                    token: 'somevalue123'
                })
                .then(function(response) {
                    console.log(response);
                    //CALL VALID FUNCTION HERE
                    this.valid(); //need to call function not reference it//throws error, how to run function here
                })
                .catch(function(error) {
                    console.log(error);
                    //Show Error Page
                });
        }
      onInIt(this.props);
    }
    render() {
        return (
            <div>
                <Spinner />
        </div>
    );
}
}

javascript reactjs function authent

关于javascript - React中如何在函数中调用函数(auth、路由),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61395761/

相关文章:

javascript - 页面 URL delima - 控制浏览器刷新

javascript - 如何访问 Rails 中 form_tag 的响应?

javascript - 继电器 : How to merge instead of override queries in nested routes?

css - 我可以使用 CSS 使边框在单击后出现然后消失吗?

r - 执行库(pkg)后执行包中的函数

javascript - ListView - 使用 jQuery 搜索带有过滤器的列表

javascript - $regex需要正则表达式错误mongodb缝合

java - before() 与空值比较时失败

javascript - 调用 API 时 redux 函数 mapDispatchToProps 错误

actionscript-3 - AS3 : declaring an "undefined" parameter for int, uint 或数字