简体   繁体   English

React Native 上根据条件的不同内容

[英]Different content on React Native based on condition

on my react-native app I would like to have premium content for people who paid a subscription.在我的 react-native 应用程序上,我想为付费订阅的人提供优质内容。

My issue is how I make the content to display as unavailable(if you are not premium ) and the same as the other content if you are premium.我的问题是如何使内容显示为不可用(如果您不是高级版),如果您是高级版,则与其他内容相同。 Basically I would like the premium content to be displayed with a "lock overlay" on it for non-premium users.基本上,我希望为非高级用户显示高级内容,并在其上显示“锁定覆盖”。

However, I do not know how I set this conditional.但是,我不知道我是如何设置这个条件的。 It is a matter of state?是 state 的问题吗? If yes where should be positioned this state considering that is unidirectional?如果是的话,考虑到它是单向的,这个 state 应该放在哪里?

Just to be clear I will have premium and non premium content需要明确的是,我将拥有优质和非优质内容

在此处输入图像描述

在此处输入图像描述



class Browser extends Component {
  scrollX = new Animated.Value(0);

  renderRecommended = () => {
    return (
      <View style={[styles.flex, styles.column, styles.recommended]}>
        <View style={[styles.row, styles.recommendedHeader]}>
          <Text
            style={{
              fontSize: theme.sizes.font * 1.4,
              alignSelf: 'center',
              color: 'white',
              fontFamily: 'Nunito-Bold',
            }}>
            Recommended
          </Text>
        </View>
        <View style={[styles.column, styles.recommendedList]}>
          <FlatList
            horizontal
            scrollEnabled
            showsHorizontalScrollIndicator={false}
            scrollEventThrottle={16}
            snapToAlignment="center"
            style={[styles.shadow, {overflow: 'visible'}]}
            data={this.props.destinations}
            keyExtractor={(item, index) => `${item.id}`}
            renderItem={({item, index}) =>
              this.renderRecommendation(item, index)
            }
          />
        </View>
      </View>
    );
  };

  renderRecommendation = (item, index) => {
    const {destinations} = this.props;
    const isLastItem = index === destinations.length - 1;
    const {navigation} = this.props;
    return (
      <TouchableOpacity
        onPress={() =>
          this.props.navigation.navigate('PreScreen', {
            item,
          })
        }>
        <View
          style={[
            styles.flex,
            styles.column,
            styles.recommendation,
            styles.shadow,
            index === 0 ? {marginLeft: theme.sizes.margin} : null,
            isLastItem ? {marginRight: theme.sizes.margin / 2} : null,
          ]}>
          <ImageBackground
            style={[styles.imageback]}
            source={{uri: item.preview}}
          />
        </View>
        <View
          style={[
            index === 0 ? {marginLeft: theme.sizes.margin - 10} : null,
            isLastItem ? {marginRight: theme.sizes.margin / 2} : null,
          ]}>
          <Text
            style={{
              fontSize: theme.sizes.font * 1.25,
              fontWeight: '200',
              color: 'white',
              marginLeft: 10,
              //paddingBottom: 20,
              fontFamily: 'Nunito-Bold',
            }}>
            {item.title}
          </Text>

          <Text
            style={{
              color: theme.colors.caption,
              marginLeft: 10,
              fontFamily: 'Nunito-SemiBold',
            }}>
            {item.location}
          </Text>
        </View>
      </TouchableOpacity>
    );
  };
render() {
    return (
      <ScrollView style={styles.container}>
        <BackgroundSvg style={styles.background} />
        <ScrollView
          style={styles.contentContainer}
          showsVerticalScrollIndicator={false}
          contentContainerStyle={{paddingBottom: theme.sizes.paddin}}>
          {this.renderRecommended()}
          {this.renderRecommended2()}
          {/* <View style={styles.mainContainerView}>
            <TouchableOpacity style={styles.singInButton} gradient>
              <Text style={styles.logInText}>
                Activate premium subscription
              </Text>
            </TouchableOpacity>
          </View> */}
        </ScrollView>
      </ScrollView>
    );
  }
}
Browser.defaultProps = {
  destinations: mocks,
  reading: readingList,
};

export default Browser;

My code is the one on the top.我的代码是顶部的代码。 Just to simplify I am accesing some elements from JSON and I am creating Flatlist based on this.只是为了简化,我正在访问 JSON 中的一些元素,我正在基于此创建平面列表。 What I want is is to give some of the JSON files a bolean with premium or not and in this way to make some elements available for user or not.我想要的是给一些 JSON 文件一个带有溢价的布尔值,并以这种方式使某些元素对用户可用或不可用。

As part of your destinations array, for each object, you can specify a boolean field called isPremiumItem .作为destinations数组的一部分,对于每个 object,您可以指定一个名为isPremiumItem的 boolean 字段。 Your users should have a boolean field like isPremiumUser to show the type of their subscription.您的用户应该有一个像isPremiumUser这样的 boolean 字段来显示他们的订阅类型。 Then in renderRecommended method you can check the user subscription status ( isPremiumUser ), and also the specific item status ( isPremiumItem ).然后在renderRecommended方法中,您可以检查用户订阅状态( isPremiumUser ),以及特定项目状态( isPremiumItem )。 Then you can render accordingly.然后你可以相应地渲染。

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

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