Javascript 如何仅在用户登录世博会时显示价格?

Javascript 如何仅在用户登录世博会时显示价格?,javascript,expo,Javascript,Expo,我有一个页面询问我的产品价格: /** @format */ import React, { PureComponent } from "react"; import PropTypes from "prop-types"; import { View, Text } from "react-native"; import { Color, withTheme, Tools } from "@common"; import styles from "./styles"; class Prod

我有一个页面询问我的产品价格:

/** @format */

import React, { PureComponent } from "react";
import PropTypes from "prop-types";
import { View, Text } from "react-native";
import { Color, withTheme, Tools } from "@common";
import styles from "./styles";

class ProductPrice extends PureComponent {
  static propTypes = {
    product: PropTypes.object,
    hideDisCount: PropTypes.bool,
    style: PropTypes.any,
  };

  render() {
    const {
      product,
      hideDisCount,
      style,
      fontStyle,
      theme: {
        colors: { text },
      },
      currency
    } = this.props;
    const productPrice = `${Tools.getPriceIncluedTaxAmount(product, null, false, currency)} `;
    const regular_price = product["multi-currency-prices"] && product["multi-currency-prices"][currency.code] ? product["multi-currency-prices"][currency.code]["price"] : product.regular_price
    const productPriceSale = product.on_sale
      ? `${Tools.getCurrecyFormatted(regular_price, currency)} `
      : null;
    return (
      <View style={[styles.price_wrapper, style && style]}>
        <Text
          style={[
            styles.text_list,
            styles.price,
            {
              color: Color.blackTextSecondary,
            },
            { color: text },
            fontStyle && fontStyle,
          ]}>
          {productPrice}
        </Text>

        {product.on_sale && <Text style={[styles.text_list, styles.sale_price, { color: text }, fontStyle && fontStyle,]}>
          {productPriceSale}
        </Text>}

        {hideDisCount ? (
          <View />
        ) : !product.on_sale ? (
          <View />
        ) : (
              <View style={styles.saleWrap}>
                <Text style={[styles.text_list, styles.sale_off, { color: text }, fontStyle && fontStyle,]}>
                  {`-${(
                    (1 - Number(product.price) / Number(product.regular_price)) *
                    100
                  ).toFixed(0)}%`}
                </Text>
              </View>
            )}
      </View>
    );
  }
}

export default withTheme(ProductPrice);

答案可能类似于
this.props.userIsLoggedIn&
。因此,只要在应用程序知道用户是否登录时渲染您想要渲染的内容即可。你可以在这里想怎么复杂就怎么复杂。但是在不知道用户是如何登录的情况下(即他们通常需要获得某种会话cookie或会话令牌,以便应用程序知道它是“是”还是“不是”),无法真正告诉atm我很抱歉。谢谢您的评论,我在另一个页面中添加了有关如何调用它的信息。我正试图弄明白,但它不起作用:(基本上我想做的是:如果用户登录,那么显示价格,否则,显示“登录购买”。但我有一个困难的时间:(基于上述内容,您应该能够在渲染中检查用户是否存在,并将其分配给变量,如
constuserisloggedin=getName(user)!==Languages.Guest
可能吗?仍然不能说出逻辑,最好使用静态
loggedIn
方法。但是,在渲染函数的返回值中使用
userIsLoggedIn&
谢谢Dan,这对我来说肯定太复杂了:(但是谢谢你的洞察力!类似:
static loggedIn的东西(user){return!!user;}
,然后在
{loggedIn(user)中包装您只想显示登录用户的位&&
/**
   * getName user
   * @user
   */
  static getName = (user) => {
    if (user != null) {
      if (
        typeof user.last_name !== "undefined" ||
        typeof user.first_name !== "undefined"
      ) {
        const first = user.first_name != null ? user.first_name : "";
        const last = user.last_name != null ? user.last_name : "";
        return `${first} ${last}`;
      } else if (typeof user.name !== "undefined" && user.name != null) {
        return user.name;
      }
      return Languages.Guest;
    }
    return Languages.Guest;
  };