Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.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 从功能组件返回变量_Javascript_Reactjs_Function - Fatal编程技术网

Javascript 从功能组件返回变量

Javascript 从功能组件返回变量,javascript,reactjs,function,Javascript,Reactjs,Function,我有一个反应组件,如下所示: class ModulesListing extends React.Component { render(){ const module = this.props.module; getModuleCode(module){ var moduleCode = 0; // Do relevant calls and get moduleCode return moduleCode; } r

我有一个反应组件,如下所示:

class ModulesListing extends React.Component {
  render(){
    const module = this.props.module;
    getModuleCode(module){
       var moduleCode = 0;
       // Do relevant calls and get moduleCode
       return moduleCode;
    }
    return (
      //Info to display
    );
  }
}
在这里,我需要在return()中获取moduleCode的值,并将其分配给一个变量以进行进一步的处理。当我被指派为

var moduleCode = this.getModuleCode(module);

它返回一个未定义的对象。从函数返回值的正确方法是什么?

尝试在
ComponentDidMount()
中调用函数

componentDidMount(){
  this.getModuleCode(this.props.module);
}
并将
moduleCode
置于状态,以便在调用
didmount
后可以接收它


在render-
this.state.moduleCode中接收它

尝试调用
组件didmount()
中的函数

componentDidMount(){
  this.getModuleCode(this.props.module);
}
并将
moduleCode
置于状态,以便在调用
didmount
后可以接收它


在render-
this.state.moduleCode

中接收状态,您可以在getModuleCode函数中定义状态并设置状态,然后在任何需要的地方使用它

class ModulesListing extends React.Component {

    componentDidMount(){
        this.getModuleCode(this.props.module);
    }

    render(){
       const module = this.props.module;
       getModuleCode = (module) => {
          var moduleCode = 0;
          // Do relevant calls and get moduleCode
          this.setState({moduleCode})
       }
       return (
            const {moduleCode} = this.state;
            //Info to display
        );
     }
}

您可以在getModuleCode函数中定义状态并设置状态,然后在任何需要的地方使用它

class ModulesListing extends React.Component {

    componentDidMount(){
        this.getModuleCode(this.props.module);
    }

    render(){
       const module = this.props.module;
       getModuleCode = (module) => {
          var moduleCode = 0;
          // Do relevant calls and get moduleCode
          this.setState({moduleCode})
       }
       return (
            const {moduleCode} = this.state;
            //Info to display
        );
     }
}

getModuleCode必须位于渲染函数之外,并通过绑定或箭头函数绑定到“this”上下文

类模块列表扩展了React.Component{
getModuleCode=(模块)=>{
常数moduleCode=0;
//拨打相关电话并获取模块代码
返回模块代码;
}
render(){
//渲染
}

}
getModuleCode必须位于渲染函数外部,并通过绑定或箭头函数绑定到“this”上下文

类模块列表扩展了React.Component{
getModuleCode=(模块)=>{
常数moduleCode=0;
//拨打相关电话并获取模块代码
返回模块代码;
}
render(){
//渲染
}

}
如果要使用
var moduleCode=this.getModuleCode(模块)
在渲染函数中,必须将
getModuleCode(module)
定义为组件类中的方法,而不是渲染函数中的方法

class ModulesListing extends React.Component {
  getModuleCode(module){
       var moduleCode = 0;
       // Do relevant calls and get moduleCode
       return moduleCode;
    }
  render(){
    const module = this.props.module;
    var moduleCode = this.getModuleCode(module);
    return (
      //Info to display
    );
  }
}

希望对您有所帮助。

如果您想使用
var moduleCode=this.getModuleCode(module)
在渲染函数中,必须将
getModuleCode(module)
定义为组件类中的方法,而不是渲染函数中的方法

class ModulesListing extends React.Component {
  getModuleCode(module){
       var moduleCode = 0;
       // Do relevant calls and get moduleCode
       return moduleCode;
    }
  render(){
    const module = this.props.module;
    var moduleCode = this.getModuleCode(module);
    return (
      //Info to display
    );
  }
}

希望能有所帮助。

您可以在
componentDidMount
中获取代码,并将其存储在state中

示例

函数doCall(){
返回新承诺(resolve=>setTimeout(()=>resolve(“code”),1000);
}
类ModuleListing扩展了React.Component{
状态={code:null};
componentDidMount(){
这个.getModuleCode();
}
getModuleCode=模块=>{
doCall(this.props.module)。然后(code=>{
this.setState({code});
});
};
render(){
const{code}=this.state;
如果(代码===null){
返回null;
}
返回{code};
}
}
render(,document.getElementById(“根”))

您可以在
componentDidMount
中获取代码,并将其存储在state中

示例

函数doCall(){
返回新承诺(resolve=>setTimeout(()=>resolve(“code”),1000);
}
类ModuleListing扩展了React.Component{
状态={code:null};
componentDidMount(){
这个.getModuleCode();
}
getModuleCode=模块=>{
doCall(this.props.module)。然后(code=>{
this.setState({code});
});
};
render(){
const{code}=this.state;
如果(代码===null){
返回null;
}
返回{code};
}
}
render(,document.getElementById(“根”))


const theCode=getModuleCode(模块)?问题不存在clear@Tholle对不起,我的问题。不过,不是working@Harikrishnan,函数getModuleCode()返回一个需要在render()方法中使用的整数。如何获取该值并存储在变量
const theCode=getModuleCode(模块)中?问题不存在clear@Tholle对不起,我的问题。不过,不是working@Harikrishnan,函数getModuleCode()返回一个需要在render()方法中使用的整数。我怎样才能得到那个值并存储在一个变量中呢?它工作得很好,还有一个例子,就像我得到一个模块列表作为道具一样。我需要运行一个循环来获取渲染中每个模块的moduleCode。在那种情况下,哪一种可能是可行的solution@usergs伟大的这完全改变了问题。请接受此问题的答案,如果您还有其他问题,请创建新问题。它工作得很好,还有一个例子,就像我得到的道具模块列表。我需要运行一个循环来获取渲染中每个模块的moduleCode。在那种情况下,哪一种可能是可行的solution@usergs伟大的这完全改变了问题。请接受此问题的答案,如果您还有其他问题,请创建新问题。尝试这种方式,当在控制台中打印moduleCode时,它表示未定义按这种方式打印,当在控制台中打印moduleCode时,它表示未定义