简体   繁体   English

在另一个 function React Native 中传递一个 function

[英]Pass a function inside another function React Native

I ve got the following component which renders an audio player.我有以下呈现音频播放器的组件。 The meditationList function gives me the location of the file that I want to play dynamically, and audio function is the functionality of player.冥想列表 function 给了我想要动态播放的文件的位置,音频 function 是播放器的功能。

What I want to do is to pass the meditation list return on track player url我想要做的是通过冥想列表返回轨道播放器 url

Thank you谢谢


export default class Player extends React.Component<Props> {
  meditationList = () => {
    const adress = this.props.route.params;

    return JSON.stringify(adress.meditation.meditationPlayerAdress);
  };
  audio = () => {
    TrackPlayer.setupPlayer().then(async () => {
      // Adds a track to the queue

      await TrackPlayer.add({
        id: 'trackId',
        url: this.require(meditationList()),
        title: 'Track Title',
        artist: 'Track Artist',
      });

      let trackId = await TrackPlayer.getCurrentTrack();

      // Starts playing it
      TrackPlayer.play();
    });
  };

  pause = () => {
    TrackPlayer.pause();
  };

  stop = () => {
    TrackPlayer.stop();
  };




  render() {
    return (
     ....)}}}

Error image错误图片

Hi Please change your code as below嗨,请更改您的代码如下

  1. Remove the duplicate function删除重复的 function
  2. Change url: require('../../../assets/track.mp3') to url: this.require(meditationList()), url: require('../../../assets/track.mp3')更改为url: this.require(meditationList()),

export default class Player extends React.Component<Props> {
  meditationList = () => {
    const adress = this.props.route.params;
    return JSON.stringify(adress.meditation.meditationPlayerAdress);
  };

  audio = () => {
    TrackPlayer.setupPlayer().then(async () => {
      // Adds a track to the queue

      await TrackPlayer.add({
        id: 'trackId',
        url: require(this.meditationList()),
        title: 'Track Title',
        artist: 'Track Artist',
      });

      let trackId = await TrackPlayer.getCurrentTrack();

      // Starts playing it
      TrackPlayer.play();
    });
  };

 pause = () => {
    TrackPlayer.pause();
  };

  stop = () => {
    TrackPlayer.stop();
  };

  render() {
    return (
     ....)}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Native-在另一个函数中无法识别该函数 - React Native - Function is not recognized inside Another Function React Native Function 外部 Class 将项目传递给 Function 内部 - React Native Function Outside Class Pass Item to Function Inside 如何在本机反应中调用另一个 function 内部的 function - how to call a function that is inside another function in react native 将一个 function 中的数据传递到 React 组件内的另一个 function - pass data in one function to another function inside a React component React Native如何在for循环中传递带有参数的函数 - React Native how to pass a function with a parameter inside a for-loop 我无法将参数传递给 React Native 上的 props 内的函数 - I can't pass arguments to a function inside props on React Native 在本机反应中将功能从一个屏幕传递到另一个屏幕 - Pass function from one screen to another in react native React Native:将字符串作为函数传递 - React native: pass string as function 一个 function 在 React Native 中的另一个内部不起作用 - One function does not work inside another in React Native 如何从 react native flatList 中的另一个函数获取值? - How to get value from another function inside react native flatList?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM