如何在ReactJS中将函数的结果返回到组件

如何在ReactJS中将函数的结果返回到组件,reactjs,Reactjs,我试图将函数的值返回到reactjs中的组件。正在显示函数中的console.log,但未返回结果。在这种情况下,我每次都会遇到麻烦。谁能帮我一下吗 下面是我的代码。我无法显示应该从函数encodedAudioFilename()返回的波形标记 const popupmodel=props=>{ ..... 一些代码 ..... 函数名(){ 常量正则表达式=/(?){ 控制台日志(“https://d30pkmxa7non58.cloudfront.net/“+encodeURICompone

我试图将函数的值返回到reactjs中的组件。正在显示函数中的console.log,但未返回结果。在这种情况下,我每次都会遇到麻烦。谁能帮我一下吗

下面是我的代码。我无法显示应该从函数encodedAudioFilename()返回的波形标记

const popupmodel=props=>{
.....
一些代码
.....
函数名(){
常量正则表达式=/(?){
控制台日志(“https://d30pkmxa7non58.cloudfront.net/“+encodeURIComponent(match));
返回
});
}
}
返回(
{encodedAudioFilename()}
)
};
导出默认的popupmodel;

函数实际上不返回任何内容

试试下面的代码

function encodedAudioFilename(){
    const regex = /(?<=recordings\/).+/gm;
    let m;

    let ar = [];
    while ((m = regex.exec(audioLocation)) !== null) {
     
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        // The result can be accessed through the `m`-variable.
        arr = m.map((match, groupIndex) => {
            console.log("https://d30pkmxa7non58.cloudfront.net/"+encodeURIComponent(match));
            
            return <Waveform key={groupIndex} src={"https://d30pkmxa7non58.cloudfront.net/"+encodeURIComponent(match) }/>
        });
        arr = [...ar, ...arr];
    }
    return ar;
  }
函数encodedAudioFilename(){
常量正则表达式=/(?){
控制台日志(“https://d30pkmxa7non58.cloudfront.net/“+encodeURIComponent(match));
返回
});
arr=[…ar,…arr];
}
返回ar;
}

首先。您必须使用列表映射功能,因为forEach不返回任何内容。非常感谢,我没有意识到我在使用forEach。使用Map解决了这个问题。
function encodedAudioFilename(){
    const regex = /(?<=recordings\/).+/gm;
    let m;

    let ar = [];
    while ((m = regex.exec(audioLocation)) !== null) {
     
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        // The result can be accessed through the `m`-variable.
        arr = m.map((match, groupIndex) => {
            console.log("https://d30pkmxa7non58.cloudfront.net/"+encodeURIComponent(match));
            
            return <Waveform key={groupIndex} src={"https://d30pkmxa7non58.cloudfront.net/"+encodeURIComponent(match) }/>
        });
        arr = [...ar, ...arr];
    }
    return ar;
  }