简体   繁体   English

本地列表的全局或静态状态

[英]Global or static State for flatlist Rows in react native

Is it possible that for all the rows of Flatlist there is one static state . 对于Flatlist所有行, Flatlist有一个静态 state I have a Component which i want to display only on the Flatlist 's one row when i click on it. 我有一个Component ,当我单击它时,它仅想显示在Flatlist的一行上。 And when i click on other row of Flatlist The Component should not render on previous row but on current clicked row. 当我单击Flatlist的另一行时,该Component不应在上一行呈现,而应在当前单击的行呈现。

2行平面清单

Here I have two Rows of Flatlist. 在这里,我有两行Flatlist。 I want To render Progress bar only for the clicked cardview or Play Buton. 我只想为单击的cardview或Play Buton渲染进度栏。 So my logic is when i click the Button Then I somehow put the keyID of clicked Flatlist 's rows in the global state which will only render the clicked Card View's progress bar. 所以我的逻辑是,当我单击“按钮”时,我以某种方式将被单击的Flatlist行的Flatlist置于全局 state ,这只会呈现被单击的Card View的进度条。 Somehow Like this code: 像这样的代码:

{  GlobalState===this.props.key && (<ProgressBar />)}

as a checker for each Row render in flatlist 作为平面列表中每个Row渲染的检查器

I ended up using global variable in component and prop of component. 我最终在component和prop的prop中使用了全局变量。

In renderItem of Flatlist : renderItemFlatlist

function in renderItem of Flatlist renderItemFlatlist函数

_play = () => {
    this.setState({ playState: "playing" });
    TrackPlayer.play();
    const thisplaying = this.props.item.Key;
    ProgressBar.setCurrentPlayer(thisplaying);//Here I call to put the Key of Item to the component's global variable
  };

using component in render of renderItem of Flatlist renderItemFlatlist render中使用组件

<ProgressBar PlayerKey={this.props.item.Key} />

In Component{ ProgressBar.js } used in renderItem of Flatlist : 在Flatlist的Flatlist使用的Component { ProgressBar.js }中:

import React from "react";
import { View, Slider, Text} from "react-native";
import TrackPlayer, { ProgressComponent } from "react-native-track-player";
import { formatTime } from "./utils";
global.MineKey = null; //Here I used Global Variable
class ProgressBar extends ProgressComponent {
  static setCurrentPlayer = player => {
    global.MineKey = player;         //Setting global variable on every call from the cardview upon play
  };

  onSliderEditStart = () => {
    TrackPlayer.pause();
  };
  onSliderEditEnd = () => {
    TrackPlayer.play();
  };
  onSliderEditing = value => {
    TrackPlayer.seekTo(value);
  };

  render() {
    const position = formatTime(Math.floor(this.state.position));
    const duration = formatTime(Math.floor(this.state.duration));
    return global.MineKey == this.props.PlayerKey ? (  //If Global variable key is equal to the Key of RowItem of Flatlist
      <View style={{ flexDirection: "row",alignItems: "center", flex: 1,width: "100%"}}>
        <Text style={{ color: "white", alignSelf: "center" }}>{position}</Text>
        <Slider
          onTouchStart={this.onSliderEditStart}
          onTouchEnd={this.onSliderEditEnd}
          onValueChange={this.onSliderEditing}
          value={this.state.position}
          maximumValue={this.state.duration}
          maximumTrackTintColor="gray"
          minimumTrackTintColor="white"
          thumbTintColor="white"
          style={{
            flex: 1,
            alignSelf: "center"
          }}
        />
        <Text style={{ color: "white", alignSelf: "center" }}>{duration}</Text>
      </View>
    ) : null;
  }
}
module.exports = ProgressBar;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM