在useEffect内部订阅在react-native中不起作用

时间:2020-06-03 04:49:45

标签: reactjs react-native branch.io

我有以下代码用于订阅branch.io。在这里,第一个console.log()被打印出来。但这还不止于此。我在这里做什么错了?

import React, { useEffect, useState } from 'react';
import { View } from 'react-native';

import branch from 'react-native-branch';

import {
  Button,
  InviteSummary,
  ListItem,
  ScreenContainer,
  Seperator,
  Label,
} from 'components';

import { APP_HOME_SCREEN } from 'common/constants/screens';
import { copyContent, shareContent } from 'library/Utils';

import ChevronRight from 'res/icons/Chevron/Right';
import Copied from 'res/icons/System/checked.svg';
import Copy from 'res/icons/System/Copy';

const SCREEN_TITLE = 'Invite and earn';
let _unsubscribeFromBranch = null;

const Invite = () => {
  const [referralLink, setReferralLink] = useState('');
  const [linkCopied, handleCopy] = useState(false);

  useEffect(() => {
    console.log('before subscribe');
    _unsubscribeFromBranch = branch.subscribe(({ error, params }) => {
      console.log('in subscribe');
      if (error) {
        console.error('Error from Branch: ', error);
        return;
      }

      console.log('Branch params: ', JSON.stringify(params));

      if (!params['+clicked_branch_link']) return;
    });
  }, []);

  const handleCopyLink = (text: string) => copyContent(text, () => handleCopy(true));

  const handleShareLink = () => shareContent(referralLink);

  return (
    <>
      <ScreenContainer
        title={SCREEN_TITLE}
        backScreen={APP_HOME_SCREEN}
        backScreenIcon={<ChevronRight />}>
        <View>
          <InviteSummary
            upperMainText='S$40.00'
            lowerMainText='earned'
            leftSubText='7 invites earned'
            rightSubText='3 pending'
          />

          <Label
            type={Label.Types.BODY_SMALL}
            label='when you invite a friend to  no matter how many friends! To make things sweeter, each friend you invite gets S$5 too.'
          />
          <Label
            type={Label.Types.BODY_SMALL}
            label='Referral credit will be awarded to you automatically once your friend activates their Visa debit card.'
          />

          <Seperator />

          <Label
            type={Label.Types.BODY_SMALL}
            label='SHARE Your unique invite link'
          />

          <ListItem
            title={referralLink}
            rightIcon={linkCopied ? <Copied /> : <Copy />}
            disabled={linkCopied}
            onPress={() => handleCopyLink(referralLink)}
          />
        </View>
      </ScreenContainer>

      <View>
        <Button
          type={Button.Types.PRIMARY}
          text='SHARE LINK'
          disabled={!linkCopied}
          onPress={handleShareLink}
        />
      </View>
    </>
  );
};

export default Invite;

0 个答案:

没有答案