Giter Club home page Giter Club logo

whatsapp-expo's Introduction

WhatsApp clone with expo and reanimated 2.0

The goal of this project is to create a simple clone of whatsapp. The focus is on using reanimated 2 to implement the animations.

Features

Building a custom Top Tab Bar with reanimated 2.0

In this project, a horizontal scrollView was used to create a custom top tab with the help of reanimated 2. This made it easier to perform UI animations based on the scrollView offset.

One of the challenging part was how to create an effect that is fired only when a screen is focused. For example when the app loads, because we are using a scrollView, all the tab screens are mounted at the same time. Including the camera. This is not ideal, the camera should only be running when the Camera screen is focused. Thankfully React Context came to the rescue

A context was created to provide the name of the active/focused screen. This was made a shared value so as to prevent unnecessary rerendering.

    export const CurrentTabScreenContext = React.createContext<SharedValue<TabRoutes>>({value: "Camera"});

A shared value activeTabScreenName is used to stored the name of the active screen via an onScroll callback with reanimated 2

     const scrollHandler = useAnimatedScrollHandler({
        onScroll: ({ contentOffset }) => {
            scrollOffset.value = contentOffset.x;
            const currentIndex = Math.abs(Math.round(scrollOffset.value / width));
            activeTabScreenName.value = TAB_SCREENS[currentIndex];
        },
  });

The scrollView was then wrapped with the CurrentTabScreenContext provider

        <CurrentTabScreenContext.Provider value={activeTabScreenName}>
            <Animated.ScrollView
            ref={scrollRef}
            onScroll={scrollHandler}
            horizontal
            snapToAlignment={"center"}
            snapToInterval={width}
            contentOffset={{ x: width, y: 0 }}
            disableIntervalMomentum
            showsHorizontalScrollIndicator={false}
            >
                <Camera />
                <Chats />
                <Status />
                <Calls />
            </Animated.ScrollView>
      </CurrentTabScreenContext.Provider>

Next a hook is created which uses CurrentTabScreenContext to check if a screen is focused

const TAB_SCREENS: TabRoutes[] = ["Camera", "Chat", "Status", "Calls"];

const useIsTopTabScreenFocused = (screenName: TabRoutes) => {
  const [isScreenFocused, setIsScreenFocused] = useState(false);
  const currentScreenName = useContext(CurrentTabScreenContext);

  useAnimatedReaction(
    () => currentScreenName.value,
    (screen) => {
      const isScreenActive = screen === screenName;
      runOnJS(setIsScreenFocused)(isScreenActive);
    }
  );

  return isScreenFocused;
};

This hook takes the name of a screen as an argument and returns a state indicating if the screen is focused or not. Using the useAnimatedReaction hook we can run a function anytime currentScreenName changes.


Getting started

  • Clone the repo

    git clone https://github.com/uwemneku/whatsapp-expo.git
  • Install dependencies

        npm install

    or

        yarn
  • Start the app

        yarn start

whatsapp-expo's People

Contributors

uwemneku avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.