xcarpentier / rn-tourguide

🚩Make an interactive step by step tour guide for your react-native app (a rewrite of react-native-copilot)
https://xcarpentier.github.io/rn-tourguide/
Other
725 stars 213 forks source link

Working example for class component #13

Open vrinch opened 3 years ago

vrinch commented 3 years ago

Is it possible you could provide a working example for for class component as my entire project was built with it and I’d like to implement an app tour instead of the regular onboarding process.

alexandrumic commented 3 years ago

hey @vrinch. I have used it like this:

  1. Created a tour context file. For example in context/ folder. In it, I've added:
import React from 'react';
import { useTourGuideController } from 'rn-tourguide';

const withTourHOC = (Component: any) => {
  return (props: any) => {
    const {
      canStart, // a boolean indicate if you can start tour guide
      start, // a function to start the tourguide
      stop, // a function  to stopping it
      eventEmitter, // an object for listening some events
    } = useTourGuideController();

    return <Component
      tourCanStart={canStart}
      tourStart={start}
      tourStop={stop}
      tourEventEmitter={eventEmitter}
      {...props} />;
  };
};

export { withTourHOC };
  1. Imported that context file in App main component:

    import { withTourHOC } from 'src/context/tour';
  2. Used it as HOC for App component:

    export default withTourHOC(App);
  3. Used it in App as follows:

Hope this helps 😄

beidaoxiangcai commented 8 months ago

I followed the instructions, but encountered an error “this.props.tourEventEmitter.on is undefined”. How can I resolve this?

Yang9322 commented 8 months ago

I followed the instructions, but encountered an error “this.props.tourEventEmitter.on is undefined”. How can I resolve this?

I have also encountered this problem.

Yang9322 commented 8 months ago

I have solved this problem, you need to wrap the corresponding Class Component with a Provider.