Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.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_React Native - Fatal编程技术网

Javascript 反应本地道具

Javascript 反应本地道具,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我是个新来的本地人。我正在制作一个音乐应用程序,我正在使用react native track player模块。但是现在,我正试图将歌曲列表作为道具从我的平面列表传递到播放器组件 因此,首先,我能够展示情绪。然后,当我通过道具/移动到歌曲列表屏幕时,我能够显示歌曲列表,它将在平面列表中显示标题和艺术家。因此,当我单击其中一个时,它将进入播放屏幕。我希望能够通过道具和播放歌曲。但这首歌不会播放和崩溃。我不确定我是否正确地传递道具。如果有人能帮我解决这个问题,我将不胜感激 这是我的Cluster1

我是个新来的本地人。我正在制作一个音乐应用程序,我正在使用react native track player模块。但是现在,我正试图将
歌曲列表
作为道具从我的
平面列表
传递到播放器组件

因此,首先,我能够展示情绪。然后,当我通过道具/移动到
歌曲列表
屏幕时,我能够显示歌曲列表,它将在
平面列表
中显示标题和艺术家。因此,当我单击其中一个时,它将进入
播放
屏幕。我希望能够通过道具和播放歌曲。但这首歌不会播放和崩溃。我不确定我是否正确地传递道具。如果有人能帮我解决这个问题,我将不胜感激

这是我的
Cluster1
屏幕(第一个屏幕)

因此,当我点击歌曲标题/艺术家时,应用程序将停止。我认为错误可能是
wait TrackPlayer.add(this.props.item.songlist)但我不确定

这是我的
播放
屏幕

import TrackPlayer from 'react-native-track-player';

export default class Play extends Component{
componentDidMount()
{
    TrackPlayer.setupPlayer().then(async () => {  
        // Adds a track to the queue
        await TrackPlayer.add(this.props.item.songlist);
        // Starts playing it
        TrackPlayer.play();  
    });
}
  onPressPlay = () => {
      TrackPlayer.play();
  };
onPressPause = () => {
      TrackPlayer.pause();
  };
render() {
return (
    <View style={styles.container}>
      <View style= {{flexDirection :'column'}}>
      <TouchableOpacity style= {styles.play} onPress = {this.onPressPlay}>
          <Text style = {{fontWeight:'bold',textAlign:'center',color:'white'}}>Play</Text>
      </TouchableOpacity>
      <TouchableOpacity style= {styles.pause} onPress = {this.onPressPause}>
          <Text style = {{fontWeight:'bold',textAlign:'center',color:'white'}}>Pause</Text>
      </TouchableOpacity>        
        </View>
      </View>
  );}}

您在FlatListItem中传递了错误的道具,现在没有歌曲列表对象,因为项目本身就是歌曲列表项目, 所以只需像这样传递它
Actions.Play({songIndex:0,item:this.props.item})}>
并在Play类中更新,方法是将
this.props.item.songlist
替换为
this.props.item

下面是完整的工作示例

import React, { Component } from 'react';
import {
  View,
  Text,
  FlatList,
  TouchableOpacity,
  SectionList,
} from 'react-native';

import TrackPlayer from 'react-native-track-player';

const ClusterData = [
  {
    title: 'Cluster1',
    data: [
      { name: 'passionate' },
      { name: 'rousing' },
      { name: 'confident' },
      { name: 'boisterous' },
      { name: 'rowdy' },
    ],
    songlist: [
      {
        id: '2222',
        url: 'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',
        title: 'Better Now',
        artist: 'Post Malone',
      },
      {
        id: '2',
        url:
          'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',
        title: 'YoungBlood',
        artist: '5SOS',
      },
    ],
  },
  {
    title: 'Cluster2',
    data: [
      { name: 'rollicking' },
      { name: 'cheerful' },
      { name: 'fun' },
      { name: 'sweet' },
      { name: 'amiable' },
      { name: 'natured' },
    ],
    songlist: [
      {
        id: '1111',
        url:
          'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',
        title: 'Summertime',
        artist: 'Yellow Claw',
      },
      {
        id: '1',
        url:
          'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',
        title: 'Despacito',
        artist: 'Luis Fonsi',
      },
    ],
  },
];

export class MusicPlayer extends Component {
  state = {
    showSongList: false,
    activeSection: null,
  };

  updateState = item => {
    this.setState(item);
  };

  render() {
    return (
      <View style={{ flex: 1, paddingTop: 20 }}>
        {this.state.showSongList ? (
          <SongList section={this.state.activeSection} />
        ) : (
          <SectionList
            renderItem={({ item, index }) => {
              return <SectionListItem item={item} index={index} />;
            }}
            renderSectionHeader={({ section }) => {
              return (
                <SectionHeader
                  section={section}
                  updateState={this.updateState}
                />
              );
            }}
            sections={ClusterData}
            keyExtractor={(item, index) => item.name}
          />
        )}
      </View>
    );
  }
}
class SectionHeader extends Component {
  render() {
    return (
      <View style={{ margin: 20, marginBottom: 10 }}>
        <Text style={{ fontWeight: 'bold' }}>{this.props.section.title}</Text>
        <TouchableOpacity
          onPress={() =>
            this.props.updateState({
              showSongList: true,
              activeSection: this.props.section,
            })
          }
        >
          <Text style={{ color: 'blue' }}> Play</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

class SectionListItem extends Component {
  render() {
    return (
      <View>
        <Text style={{ margin: 5, marginLeft: 20, fontStyle: 'italic' }}>
          {this.props.item.name}
        </Text>
      </View>
    );
  }
}
export class SongList extends Component {
  state = {
    isPlayActive: false,
  };

  startPlay = data => {
    this.setState({
      isPlayActive: true,
      data: data,
    });
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <FlatList
          data={this.props.section.songlist}
          renderItem={({ item, index, rowId }) => {
            return (
              <FlatListItem
                item={item}
                index={index}
                startPlay={this.startPlay}
              />
            );
          }}
        />
        {this.state.isPlayActive && <Play {...this.state.data} />}
      </View>
    );
  }
}
class FlatListItem extends Component {
  render() {
    return (
      <View style={{ backgroundColor: '#555', height: 80, marginTop: 20 }}>
        <TouchableOpacity
          onPress={() =>
            this.props.startPlay({
              songIndex: 0,
              item: this.props.item,
            })
          }
        >
          <Text
            style={{
              paddingTop: 10,
              textAlign: 'center',
              fontSize: 20,
              color: '#FFF',
            }}
          >
            {this.props.item.title}
          </Text>
          <Text
            style={{
              textAlign: 'center',
              fontSize: 15,
              paddingTop: 10,
              color: '#FFF',
            }}
          >
            {this.props.item.artist}
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

export class Play extends Component {
  componentDidMount() {
    TrackPlayer.setupPlayer().then(async () => {
      // Adds a track to the queue
      await TrackPlayer.add(this.props.item);
      // Starts playing it
      TrackPlayer.play();
    });
  }
  onPressPlay = () => {
    TrackPlayer.play();
  };
  onPressPause = () => {
    TrackPlayer.pause();
  };
  render() {
    return (
      <View style={{ height: 400 }}>
        <View style={{ flexDirection: 'row' }}>
          <TouchableOpacity
            style={{
              backgroundColor: '#f0f0f0',
              height: 40,
              flex: 1,
              alignItems: 'center',
              justifyContent: 'center',
              marginRight: 10,
            }}
            onPress={this.onPressPlay}
          >
            <Text
              style={{
                fontWeight: 'bold',
                textAlign: 'center',
                color: 'black',
              }}
            >
              Play
            </Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={{
              flex: 1,
              backgroundColor: '#f0f0f0',
              height: 40,
              alignItems: 'center',
              justifyContent: 'center',
            }}
            onPress={this.onPressPause}
          >
            <Text
              style={{
                fontWeight: 'bold',
                textAlign: 'center',
                color: 'black',
              }}
            >
              Pause
            </Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}
import React,{Component}来自'React';
进口{
看法
文本,
平面列表,
可触摸不透明度,
章节列表,
}从“反应本机”;
从“react native track player”导入TrackPlayer;
常量群集数据=[
{
标题:“Cluster1”,
数据:[
{姓名:'激情'},
{name:'rousing'},
{name:'自信'},
{name:'喧闹'},
{name:'rowdy'},
],
歌曲列表:[
{
id:'2222',
网址:'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',
标题:“现在更好”,
艺术家:《后马龙》,
},
{
id:'2',
网址:
'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',
标题:“年轻人”,
艺术家:“5SOS”,
},
],
},
{
标题:“Cluster2”,
数据:[
{name:'rollicking'},
{姓名:'开朗'},
{name:'fun'},
{name:'sweet'},
{name:'和蔼可亲'},
{name:'natured'},
],
歌曲列表:[
{
id:'1111',
网址:
'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',
标题:“夏季”,
艺术家:《黄爪》,
},
{
id:'1',
网址:
'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',
标题:“Despacito”,
艺术家:“路易斯·丰西”,
},
],
},
];
导出类MusicLayer扩展组件{
状态={
歌曲列表:假,
activeSection:null,
};
updateState=项目=>{
本.设置状态(项目);
};
render(){
返回(
{this.state.showSongList(
) : (
{
返回;
}}
renderSectionHeader={({section})=>{
返回(
);
}}
节={ClusterData}
keyExtractor={(项,索引)=>item.name}
/>
)}
);
}
}
类SectionHeader扩展组件{
render(){
返回(
{this.props.section.title}
这是道具({
歌曲列表:没错,
activeSection:this.props.section,
})
}
>
玩
);
}
}
类SectionListItem扩展组件{
render(){
返回(
{this.props.item.name}
);
}
}
导出类歌曲列表扩展组件{
状态={
isPlayActive:false,
};
startPlay=数据=>{
这是我的国家({
是的,
数据:数据,
});
};
render(){
返回(
{
返回(
);
}}
/>
{this.state.isPlayActive&&}
);
}
}
类FlatListItem扩展组件{
render(){
返回(
这个。道具。开始播放({
歌曲索引:0,
项目:this.props.item,
})
}
>
{this.props.item.title}
{this.props.item.artist}
);
}
}
导出类播放扩展组件{
componentDidMount(){
TrackPlayer.setupPlayer()。然后(异步()=>{
//将轨迹添加到队列中
等待TrackPlayer.add(这个.props.item);
//开始演奏
TrackPlayer.play();
});
}
onPressPlay=()=>{
TrackPlayer.play();
};
按暂停=()=>{
TrackPlayer.pause();
};
render(){
返回(
玩
暂停
);
}
}

您是否尝试在componentDidMount Play组件中记录this.props.item.songlist?和。。游戏做什么动作?什么是Actions?Actions.Play来自react本机路由器流量。它将进入播放文件/屏幕。我遵循了您的代码,但我不确定为什么当我单击平面列表时会得到“不幸的是,我的项目已停止。我发布了it@OhsnapItskhai你还可以分享android studio的logcat中打印的错误消息吗?你的方法似乎是有效的。当我编写data={ClusterData}时,它可以工作。但是,当我从Cluster1屏幕(刚刚编辑)传递道具并写入数据={this.props.section.songlist}时,当我单击列表时,它会崩溃。基本上从
Cluster1
screen到
Songlist
screen和th
import TrackPlayer from 'react-native-track-player';

export default class Play extends Component{
componentDidMount()
{
    TrackPlayer.setupPlayer().then(async () => {  
        // Adds a track to the queue
        await TrackPlayer.add(this.props.item.songlist);
        // Starts playing it
        TrackPlayer.play();  
    });
}
  onPressPlay = () => {
      TrackPlayer.play();
  };
onPressPause = () => {
      TrackPlayer.pause();
  };
render() {
return (
    <View style={styles.container}>
      <View style= {{flexDirection :'column'}}>
      <TouchableOpacity style= {styles.play} onPress = {this.onPressPlay}>
          <Text style = {{fontWeight:'bold',textAlign:'center',color:'white'}}>Play</Text>
      </TouchableOpacity>
      <TouchableOpacity style= {styles.pause} onPress = {this.onPressPause}>
          <Text style = {{fontWeight:'bold',textAlign:'center',color:'white'}}>Pause</Text>
      </TouchableOpacity>        
        </View>
      </View>
  );}}
const ClusterData = [
{ title: 'Cluster1', 
data: 
[
  {name: 'passionate'},
  {name: 'rousing'},
  {name: 'confident'},
  {name: 'boisterous'},
  {name: 'rowdy'}
],
 songlist:
[
  {
    id: '2222',
    url: 'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',
    title: 'Better Now',
    artist: 'Post Malone',
  },
  {
    id: '2',
    url: 'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',
    title: 'YoungBlood',
    artist: '5SOS',
  },
]
},
{ title: 'Cluster2', 
  data: 
[
  {name: 'rollicking'},
  {name: 'cheerful'},
  {name: 'fun'},
  {name: 'sweet'},
  {name: 'amiable'},
  {name: 'natured'}
],
  songlist:
  [
    {
      id: '1111',
      url: 'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',
      title: 'Summertime',
      artist: 'Yellow Claw',
    },
    {
      id: '1',
      url: 'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',
      title: 'Despacito',
      artist: 'Luis Fonsi',
    },
  ]
},
import React, { Component } from 'react';
import {
  View,
  Text,
  FlatList,
  TouchableOpacity,
  SectionList,
} from 'react-native';

import TrackPlayer from 'react-native-track-player';

const ClusterData = [
  {
    title: 'Cluster1',
    data: [
      { name: 'passionate' },
      { name: 'rousing' },
      { name: 'confident' },
      { name: 'boisterous' },
      { name: 'rowdy' },
    ],
    songlist: [
      {
        id: '2222',
        url: 'http://tegos.kz/new/mp3_full/Post_Malone_-_Better_Now.mp3',
        title: 'Better Now',
        artist: 'Post Malone',
      },
      {
        id: '2',
        url:
          'http://tegos.kz/new/mp3_full/5_Seconds_Of_Summer_-_Youngblood.mp3',
        title: 'YoungBlood',
        artist: '5SOS',
      },
    ],
  },
  {
    title: 'Cluster2',
    data: [
      { name: 'rollicking' },
      { name: 'cheerful' },
      { name: 'fun' },
      { name: 'sweet' },
      { name: 'amiable' },
      { name: 'natured' },
    ],
    songlist: [
      {
        id: '1111',
        url:
          'http://tegos.kz/new/mp3_full/Yellow_Claw_and_San_Holo_-_Summertime.mp3',
        title: 'Summertime',
        artist: 'Yellow Claw',
      },
      {
        id: '1',
        url:
          'http://tegos.kz/new/mp3_full/Luis_Fonsi_feat._Daddy_Yankee_-_Despacito.mp3',
        title: 'Despacito',
        artist: 'Luis Fonsi',
      },
    ],
  },
];

export class MusicPlayer extends Component {
  state = {
    showSongList: false,
    activeSection: null,
  };

  updateState = item => {
    this.setState(item);
  };

  render() {
    return (
      <View style={{ flex: 1, paddingTop: 20 }}>
        {this.state.showSongList ? (
          <SongList section={this.state.activeSection} />
        ) : (
          <SectionList
            renderItem={({ item, index }) => {
              return <SectionListItem item={item} index={index} />;
            }}
            renderSectionHeader={({ section }) => {
              return (
                <SectionHeader
                  section={section}
                  updateState={this.updateState}
                />
              );
            }}
            sections={ClusterData}
            keyExtractor={(item, index) => item.name}
          />
        )}
      </View>
    );
  }
}
class SectionHeader extends Component {
  render() {
    return (
      <View style={{ margin: 20, marginBottom: 10 }}>
        <Text style={{ fontWeight: 'bold' }}>{this.props.section.title}</Text>
        <TouchableOpacity
          onPress={() =>
            this.props.updateState({
              showSongList: true,
              activeSection: this.props.section,
            })
          }
        >
          <Text style={{ color: 'blue' }}> Play</Text>
        </TouchableOpacity>
      </View>
    );
  }
}

class SectionListItem extends Component {
  render() {
    return (
      <View>
        <Text style={{ margin: 5, marginLeft: 20, fontStyle: 'italic' }}>
          {this.props.item.name}
        </Text>
      </View>
    );
  }
}
export class SongList extends Component {
  state = {
    isPlayActive: false,
  };

  startPlay = data => {
    this.setState({
      isPlayActive: true,
      data: data,
    });
  };

  render() {
    return (
      <View style={{ flex: 1 }}>
        <FlatList
          data={this.props.section.songlist}
          renderItem={({ item, index, rowId }) => {
            return (
              <FlatListItem
                item={item}
                index={index}
                startPlay={this.startPlay}
              />
            );
          }}
        />
        {this.state.isPlayActive && <Play {...this.state.data} />}
      </View>
    );
  }
}
class FlatListItem extends Component {
  render() {
    return (
      <View style={{ backgroundColor: '#555', height: 80, marginTop: 20 }}>
        <TouchableOpacity
          onPress={() =>
            this.props.startPlay({
              songIndex: 0,
              item: this.props.item,
            })
          }
        >
          <Text
            style={{
              paddingTop: 10,
              textAlign: 'center',
              fontSize: 20,
              color: '#FFF',
            }}
          >
            {this.props.item.title}
          </Text>
          <Text
            style={{
              textAlign: 'center',
              fontSize: 15,
              paddingTop: 10,
              color: '#FFF',
            }}
          >
            {this.props.item.artist}
          </Text>
        </TouchableOpacity>
      </View>
    );
  }
}

export class Play extends Component {
  componentDidMount() {
    TrackPlayer.setupPlayer().then(async () => {
      // Adds a track to the queue
      await TrackPlayer.add(this.props.item);
      // Starts playing it
      TrackPlayer.play();
    });
  }
  onPressPlay = () => {
    TrackPlayer.play();
  };
  onPressPause = () => {
    TrackPlayer.pause();
  };
  render() {
    return (
      <View style={{ height: 400 }}>
        <View style={{ flexDirection: 'row' }}>
          <TouchableOpacity
            style={{
              backgroundColor: '#f0f0f0',
              height: 40,
              flex: 1,
              alignItems: 'center',
              justifyContent: 'center',
              marginRight: 10,
            }}
            onPress={this.onPressPlay}
          >
            <Text
              style={{
                fontWeight: 'bold',
                textAlign: 'center',
                color: 'black',
              }}
            >
              Play
            </Text>
          </TouchableOpacity>
          <TouchableOpacity
            style={{
              flex: 1,
              backgroundColor: '#f0f0f0',
              height: 40,
              alignItems: 'center',
              justifyContent: 'center',
            }}
            onPress={this.onPressPause}
          >
            <Text
              style={{
                fontWeight: 'bold',
                textAlign: 'center',
                color: 'black',
              }}
            >
              Pause
            </Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}