Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从React代码向jquery函数传递参数值_Javascript_Jquery_Reactjs - Fatal编程技术网

Javascript 从React代码向jquery函数传递参数值

Javascript 从React代码向jquery函数传递参数值,javascript,jquery,reactjs,Javascript,Jquery,Reactjs,我有一个状态变量声明为: this.state = { userId: this.props.uid } 我们需要调整jquery命令,以便它可以从react函数内部接收this.state.userId。有没有一个通用的方法来做到这一点 简化: 反应 render() { return (<div id="user" data-userId={this.state.userId}></div>); } 然后你需要跳胜利舞,

我有一个状态变量声明为:

    this.state = {
         userId: this.props.uid

    }
我们需要调整jquery命令,以便它可以从react函数内部接收this.state.userId。有没有一个通用的方法来做到这一点

简化:

反应

render() {
    return (<div id="user" data-userId={this.state.userId}></div>);
}

然后你需要跳胜利舞,因为原因。(你说的“jquery命令”是什么意思?this.state应该声明为
this.state={userId:this.props.uid}
但是,是否可以调用$(“#user”).data('userId'));来自jsx代码内部?不知道,这取决于您的设置。如果您在dom上的组件外部有一个组件呈现到元素和一个jquery脚本,那么这将起作用。但是!!当react引导并在
$(document.ready()中包装时
当react重新呈现时,不会触发事件。您试图实现什么,因为您可能不需要jquery而只需使用react即可实现。有人给我们留下了一个jquery表单,该表单将使用该id来获取通过该表单显示的用户信息。您认为您的想法在这种情况下有效吗?谢谢,我想可能不行t使用
fetch
从REST类型端点获取用户信息。我将更新我的答案
$("#user").data('userId');
componentWillMount = async () => {
    const response = await fetch(`users/${this.state.userId}`, {Authentication: `Bearer ${authToken}`, 'Content-Type': 'application/json'});
    if (response.status ===200) {
        //do better checks and stuff 
        try{
            const user = await response.json();
            this.setState({
                user: user,
            });
        } catch(e){
            console.error('error with your response: '+e.message);
        }
     } else {
         console.error('response status was '+response.status);
     }
}