Bookmark action in React Native using FlatList

Bookmark action in React Native Using FlatList

tl;dr

Demonstrate fetching data(500 items) and put into a FlatList and bookmark selected items.

Outline

  1. Displaying a list of items and fetch the source from HackerNews API
  2. Component that implement shouldComponentUpdate
  3. Function to handling bookmark action (Saving the selected items)
  4. Enhance bookmark action using Set from ES6

What we use in this tutorial

  1. React Native Element
  2. PureComponent (For FlatList optimization)
  3. React Lifecycle (componentDidMount, componentDidUpdate and shouldComponentUpdate)
  4. Set in ES6

Fetching data source and display in FlatList

First, let’s create file called StoryList.js and a simple class component that extends PureComponent to optimize <FlatList />.

import React, { PureComponent } from "react";

// Flow-type
type Props = {};

type State = {
  stories: Array<number>,
  bookmarks: Array<number>,
  selectedIndex: number,
};

// For ButtonGroups
const storyTypes = [
  { name: "New", path: "newstories.json" },
  { name: "Top", path: "topstories.json" },
  { name: "Best", path: "beststories.json" },
];

// Default story type
const NEW_STORIES = 0;
class StoryList extends PureComponent<Props, State> {
  constructor(props: Props) {
    super(props);

    // Initialize default value for state
    // Separate data source and bookmarks, instead modify the data source
    this.state = {
      stories: [],
      bookmarks: [],
      selectedIndex: NEW_STORIES,
    };
  }

  render() {
    const { bookmarks, stories, selectedIndex } = this.state;

    // render our view, now it return null
    return null;
  }
}

export default StoryList;

Now, we will implement the React lifecycle – componentDidMount. Before going deeper, let has a function to handle the data fetching first.

fetchStories()

  fetchStories = async (storyType: string) => {
    try {
      const response = await fetch(
        `https://hacker-news.firebaseio.com/v0/${storyType}?print=pretty`,
      ).then(res => res.json());

      if (response) {
        this.setState({
          stories: [...response],
        });
      }
    } catch (error) {
      // handling error
    }
  };

componentDidMount()

  // When a component mounted, it will execute this lifecycle for once
  componentDidMount() {
    const { selectedIndex } = this.state;
    this.fetchStories(storyTypes[selectedIndex].path);
  }

Import the following from respectively library.

import { FlatList } from "react-native";
import { ButtonGroup, ListItem, Icon } from "react-native-elements";

render() – render the UI. Modify the render() method inside the class to the following. this.renderNews has error ? No worry will implement it next.

  render() {
    const { bookmarks, stories, selectedIndex } = this.state;

    return (
      <>
        <ButtonGroup
          buttons={storyTypes.map(storyType => storyType.name)}
          selectedIndex={selectedIndex}
          onPress={(index: number) => {
            this.setState({
              selectedIndex: index,
            });
          }}
        />
        {bookmarks.length > 0 ? (
          <ListItem
            title="Bookmarks"
            rightIcon={<Icon name="keyboard-arrow-right" />}
            containerStyle={{
              padding: 10,
              marginRight: 15,
              marginLeft: 15,
              backgroundColor: "rgba(200, 200, 200, 0.1)",
            }}
            badge={{ value: bookmarks.length }}
          />
        ) : null}
        <FlatList
          data={stories}
          renderItem={this.renderNews}
          keyExtractor={(item, index) => `list-${index}-${item}`}
          initialNumToRender={10}
          extraData={bookmarks} // this is needed to trigger re-rendering
        />
      </>
    );
  }

renderNews()

  renderNews = ({ item }: { item: number }) => {
    const { bookmarks } = this.state;
    return (
      <NewsItem
        id={item}
        // We will find the id that being bookmark, through below method
        isSelected={!!bookmarks?.find((bookmark: number) => bookmark === item)}
        onPressBookmark={this.handleOnPressBookmark(item)}
        onPressStory={details => {
          console.log(details);
        }}
      />
    );
  };

NewsItem – Implement shouldComponentUpdate

Create a new file called NewsItem and a class component.

import React, { Component } from "react";
import { View, Text, TouchableOpacity, StyleSheet } from "react-native";
import { Card, ListItem, Icon } from "react-native-elements";
import moment from "moment";

const style = StyleSheet.create({
  cardHeader: {
    alignItems: "center",
    justifyContent: "space-between",
    flexDirection: "row",
    paddingBottom: 10,
  },
});

type Props = {
  id: number,
  isSelected: boolean,
  onPressBookmark: () => void,
  onPressStory: (details: Object) => void,
};

type State = {
  details: Object,
};
class NewsItem extends Component<Props, State> {
  constructor(props: Props) {
    super(props);

    this.state = {
      details: {},
    };
  }

  componentDidMount() {
    const { id } = this.props;
    this.fetchStoryDetails(id);
  }

  shouldComponentUpdate(prevProps: Props, prevState: State) {
    const { isSelected } = this.props;
    const { details } = this.state;

    return details !== prevState.details || isSelected !== prevProps.isSelected;
  }

  fetchStoryDetails = async (id: number) => {
    try {
      const response = await fetch(
        `https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`,
      ).then(res => res.json());

      if (response) {
        this.setState({ details: { ...response } });
      }
    } catch (error) {
      // handling error
    }
  };

  handleOnPressBookmark = () => {
    const { onPressBookmark } = this.props;
    if (onPressBookmark) {
      onPressBookmark();
    }
  };

  handleOnPressStory = () => {
    const { onPressStory } = this.props;
    const { details } = this.state;

    if (onPressStory) {
      onPressStory(details);
    }
  };

  render() {
    const { details } = this.state;
    const { isSelected } = this.props;
    return (
      <Card>
        <View style={style.cardHeader}>
          <Text>{`${moment.unix(details.time).format("DD MMM YYYY")}`}</Text>
          {isSelected ? (
            <Icon name="bookmark" onPress={this.handleOnPressBookmark} />
          ) : (
            <Icon name="bookmark-border" onPress={this.handleOnPressBookmark} />
          )}
        </View>
        <TouchableOpacity onPress={this.handleOnPressStory}>
          <ListItem
            topDivider
            title={details.title}
            subtitle={`By ${details.by}`}
          />
        </TouchableOpacity>
      </Card>
    );
  }
}

export default NewsItem;

Take a closer look at the shouldComponentUpdate. shouldComponentUpdate determines whether a component should re-render or not.

  // By default this lifecycle will return true
  shouldComponentUpdate(prevProps: Props, prevState: State) {
    const { isSelected } = this.props;
    const { details } = this.state;

    // Now, we restrict the condition to only re-render
    // when the following value had changed
    return details !== prevState.details || isSelected !== prevProps.isSelected;
  }

Implement function for bookmark action

Next, go back to StoryList.js file. Then, implement the function for the bookmark action.

// import the NewsItem component
import NewsItem from "NewsItem";

class StoryList extends PureComponent {
  ...

  handleOnPressBookmark = (id: number) => {
    return () => {
       const { bookmarks } = this.state;
       const tmpBookmarks = [...bookmarks]; // clone the array

       // Checking whether the selected "id" exist in the bookmarks list
       if (tmpBookmarks.find((tmpBookmark: number) => tmpBookmark === id)) {
         // Remove the selected "id" if it existed
         const removeBookmark = tmpBookmarks.filter(
           (tmpBookmark: number) => tmpBookmark !== id,
         );

         // update state
         this.setState({
           bookmarks: [...removeBookmark],
         });
       } else {
         // otherwise, push it to the bookmarks list
         tmpBookmarks.push(id);

         this.setState({
           bookmarks: [...tmpBookmarks],
         });
       }
    };
  };
  
  ...
}

Below is the sample. For this demo. (Forget about the SearchBar, is not included in this tutorial)

Enhance the bookmark action by using Set in ES 6

Now, let us refactor the handleOnPressBookmark() as below.

  constructor() {
     this.state = {
       ...,
       bookmarks: new Set([]) // initialize to Set instead of Array
     }
  }

  handleOnPressBookmark = (id: number) => {
    return () => {
      const { bookmarks } = this.state;
      const tmpBookmarks = new Set(bookmarks);

      // Now, the code is easier to read and understand
      // And we save the system resource from looping array
      // Checking whether has the "id"
      if (tmpBookmarks.has(id)) {
        tmpBookmarks.delete(id); // remove it, if existed in the bookmarks
      } else {
        tmpBookmarks.add(id); // otherwise, add into the bookmarks
      }

      this.setState({
        bookmarks: tmpBookmarks,
      });

      // Below is previous version, for easier comparison
      // if (tmpBookmarks.find((tmpBookmark: number) => tmpBookmark === id)) {
      //   const removeBookmark = tmpBookmarks.filter(
      //     (tmpBookmark: number) => tmpBookmark !== id,
      //   );

      //   this.setState({
      //     bookmarks: [...removeBookmark],
      //   });
      // } else {
      //   tmpBookmarks.push(id);

      //   this.setState({
      //     bookmarks: [...tmpBookmarks],
      //   });
      // }
    };
  };

Access bookmarks.size is similar to Array().length

<ListItem
  title="Bookmarks"
  rightIcon={<Icon name="keyboard-arrow-right" />}
  containerStyle={{
    padding: 10,
    marginRight: 15,
    marginLeft: 15,
    backgroundColor: "rgba(200, 200, 200, 0.1)",
  }}
  badge={{ value: bookmarks.size }}
/>

Refactor renderNews() too

  renderNews = ({ item }: { item: number }) => {
    const { bookmarks } = this.state;
    return (
      <NewsItem
        id={item}
        // isSelected={!!bookmarks?.find((bookmark: number) => bookmark === item)}
        // refactor to below format
        isSelected={bookmarks.has(item)}
        onPressBookmark={this.handleOnPressBookmark(item)}
        onPressStory={details => {
          console.log(details);
        }}
      />
    );
  };

Bonus

Implement data fetching in componentDidUpdate. This is related to the onPress function in ButtonGroup. The onPress function only update selectedIndex without further action.

  componentDidUpdate(prevProps: Props, prevState: State) {
    const { selectedIndex } = this.state;

    // Then, we can handle the action in componentDidUpdate
    // Whenever a component re-render, the lifecycle will triggered
    // Thus, we can handle some action here after re-render
    // **Always do comparison, to prevent infinite loop
    if (prevState.selectedIndex !== selectedIndex) {
      this.fetchStories(storyTypes[selectedIndex].path);
    }
  }

Takeaway

  • Utilize React lifecycle – componentDidMount, shouldComponentUpdate, componentDidUpdate
  • The idea to implement a bookmark or similar item selection using FlatList in React Native
  • Using Set to replace array

Thanks for reading. Have a nice day.

Getting Started In React Native

Getting Started in React Native

tl;dr

Some personal experiences on React Native stack. The things that a developer should know. (Not need to know all at once, just for reference, where to go next)

Outline

  1. Tools
  2. Library
  3. Idea

#Tools

  • Programming Language

Mainly in JavaScript – ES6. Here, a resource for learning ES6 feature. Since, React Native is able to expose native iOS and Andriod functionality to JavaScript. In some case, developer should know the programming language in both platform, such as Objective-C or Swift and Java or Kotlin. For example, some localize payment gateway due to the provider only release native SDK. Then, developers have to create the “bridge” to expose the function from native SDK to JavaScript. In order to implement it into React Native.

Second, TypeScript – static typing programming language. You can opt-in for using TypeScript to create React Native mobile application.


  • Flow

Optional library for JavaScript to static typing. Which help developer reduce run-time and compiled error. Generally help in produce a more stable and debuggable JavaScript application.


  • Continuous Integration and Continuous Delivery (CI/CD)

Get to know which CI/CD would help you or your project. Automated your deployment to Play Store and App Store can save tons of time from work flow. Nevercode, CircleCI or Bitrise is CI/CD platform that you can try to automated your React Native deployment. Sign up an account from Bitrise or CircleCI as both of them provided free plan with limitation for developer.


Provide update over the air without go through App Store and Play Store as long as the update don’t violate the App Store and Play Store T&C and modify the purpose of your app.

#Library

  • ReactJS

It’s good to know the ReactJS before start React Native. Why ? To avoid writing crappy code that will bring tech debt to the code base. At least, developers should know the lifecycle, state, props and rendering in ReactJS. Get to know how it work, definitely make your life better.


  • UI Library

UI library is a collection of components that is styled with pre-define themes. More or less similar to Bootstrap or Foundation for web. Some of the famous UI library is Native Base, React Native Paper (Material UI theme) , React Native Elements and etc. (Personally, I would recommend Native Base as in the library quite mature. But, be aware of some pitfall from Native Base)


#Idea

  • Move fast and break things.

A famous quote from Facebook. ReactJS and React Native are inherit the spirit from the quote. Both library is moving fast, break some stuffs (Early version of React Native) and introduce new tools or API. So, if you are looking for tutorial. Please look for those new tutorial instead of month(s) ago or year(s) ago tutorial. Keep yourself updated from ReactJS blog or React Native blog. (Stop using componentWillMount for newbie, it’s deprecated)


  • File Structure

Learn to know the size of your app or discuss with your team first. It help you have a better overview to structure your folders and files.


Get to know the idea of Closure. It’s help you understand the functional programming pattern. It do help you understand how to create a Higher order Component using Closures but for component.


A good technique for optimizing React Native application. Only load specific resource when needed.


  • Composition

Get yourself familiar with the idea of composition instead of inheritance. For example, it’s sort of like playing lego. Joining small pieces of lego to build something.


Conclusion

Basically, this is a list of my personal experience regarding React Native stack. It’s not limited to this list. The rest, such as how to manage environment for dev, staging or production ? How to manage configuration in build.gradle or plist.info file ? Unit testing, end to end testing.

Out of topic, Why choose React Native ? My answer is because of declarative UI.

Thanks for reading. Have a nice day.

Custom React Hooks

Reusable Custom React Hooks

tl;dr

Create a custom React Hooks that can be share among ReactJS and React Native codebase.

A custom React Hooks for functional component. The purpose is more or less similar to my previous post Reusable HoC – Part 1 and Resuable HoC – Part 2.

This post is assuming you have a basic understanding about React Hooks. As this post will not explain what is React Hooks.

Outline

Let’s start

Step 1 – Use React Hooks in functional component

Let’s start, create a file and give it a name called demo.js or whatever you like.

import React, { useState, useEffect } from "react";
import { FlatList } from "react-native";
import { Container, Content } from "native-base";
import NewsItemWithAPIUsingHooks from "../components/NewsItemWithAPIUsingHooks";

export default function DemoReactHooks() {
  const [topStory, setTopStory] = useState([]);

  useEffect(() => {
    async function fetchTopStories() {
      try {
        const res = await fetch(`https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty`, {
          method: "GET",
        }).then(res => res.json());

        if (res) {
          setTopStory([...res]);
        }
      } catch (error) {
        // handling error
      }
    }

    fetchTopStories();
  }, []);

  function renderStory({ item, }: { item: Object }) {
    return (
      <NewsItemWithAPIUsingHooks storyID={item} />
    );
  }

  return (
    <Container>
      <Content>
        <FlatList data={topStory} keyExtractor={(item, index) => `${item}-${index}`} renderItem={renderStory} />
      </Content>
    </Container>
  );
}

As the code show above, we fetch the topstory news at useEffect which is when the component mounted. Then, display the result – a list of story Id in the <FlatList />.

What if we want to reuse the same stateful logic somewhere else or maybe sharing the same codebase with React Native ?

  • Copy paste
  • Custom React Hooks

Step 2 – Extracts React Hooks into custom React Hooks

Well, let’s extract current structure into a custom React Hooks for reuse purpose.

Now, create a new file called useStory.js and copy paste below code into the file.

import { useState, useEffect } from "react";

export default function useStory(storyType: string) {
  const [response, setResponse] = useState({});
  const [isLoading, setIsLoading] = useState(false);
  const [hasError, setHasError] = useState(false);

  useEffect(() => {
    async function fetchStory(storyType: string) {
      try {
        setIsLoading(true);
        const res = await fetch(`https://hacker-news.firebaseio.com/v0/${storyType}.json?print=pretty`, { method: "GET" }).then(res => res.json());

        if (res) {
          setResponse(res);
          setIsLoading(false);
        }
      } catch (error) {
        // handling error
        setHasError(true);
      }
    }

    fetchStory(storyType);

    return () => {
      // componentWillUnmount, cancel API request or event listener
      // To prevent memory leaked
    };
  }, [storyType]);

  return {
    response,
    isLoading,
    hasError
  };
}

Now, we extract the stateful logic into a custom Hooks. The naming convention of custom React Hooks should prefix with use. There is a eslint plugins to help developer linting the naming of custom React Hooks.

useEffect – fetching API request and return a function that will be execute when the component unmount. Cancel an API request or remove event listener will be done inside return function.

Access to the state ? Return the state at the end of the custom React Hooks.

Step 3 – Refactor component to use custom React Hooks

import React from "react";
import { FlatList } from "react-native";
import { Container, Content } from "native-base";
import NewsItemWithAPIUsingHooks from "../components/NewsItemWithAPIUsingHooks";
import useStory from "../hooks/useStory";

export default function DemoReactHooks() {
  const topstories = useStory("topstories");

  function renderStory({ item, }: { item: Object }) {
    return (
      <NewsItemWithAPIUsingHooks storyID={item} />
    );
  }

  return (
    <Container>
      <Content>
        <FlatList data={topstories.response} keyExtractor={(item, index) => `${item}-${index}`} renderItem={renderStory} />
      </Content>
    </Container>
  );
}

Now, it’s much more cleaner than before. useStory React Hooks is able to reuse in other component even in React Native without copy paste.

Conclusion

Finally, come to an end of the series of creating reusable stateful business logic, to share codebase among web and mobile app (ReactJS and React Native).

To maximize the reusability of HoC or custom React Hooks, it’s better to keep it as simple as possible and not coupling too much third party JavaScript library.

A reusable HoC or custom React Hooks is good for Junior developer or someone who is new to a project’s codebase or team. This way can reduce the time to study and get familiar with the codebase and make them onboard as soon as possible. They can be more focus on the input and output.

Maintenance issue. Now, fixed a bug in business logic only done once instead of twice or more.

Separation of concern. Separate business logic out of the presentation view. Easier for unit testing and update UI especially when the component grow larger

Thanks for reading. Full source code can be obtain from my github repository

Higher Order Component - Part 2

Higher order Component in React Native – Part 2

tl;dr

In my previous post, I explain about write a HoC, encourage more reuse of codebase. This post I will try to explain more example, to make a HoC more flexible (certain use case).

Dynamic Props

import React, { Component } from "react";

export default function withTopStories(WrappedComponent) {
  return class extends Component {
    constructor(props) {
      super();

      this.state = {
        stories: []
      }
    }
    
    componentDidCatch() {
      // error handling
    }

    async componentDidMount() {
      try {
        const res = await fetch('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty', { method: "GET" }).then(res => res.json());

        if (res) {
          this.setState({ stories: res })
        }
      } catch (error) {
        // error handling
      }
    }

    render() {
      const { stories } = this.state;
      return <WrappedComponent {...this.props} stories={stories} />
    }
  }
}

Given example above from my previous post. We facing an issue as following:

  • It’s only allow developer to fetch topstories resource from HackerNews API. (may not be a bad case, sometimes)
// latest item from HackerNews API
https://hacker-news.firebaseio.com/v0/maxitem.json?print=pretty

// Up to 500 top stories / new stories / best stories
https://hacker-news.firebaseio.com/v0/[topstories|newstories|beststories].json?print=pretty

// Up to 200 Ask / Job / Show stories
https://hacker-news.firebaseio.com/v0/[askstories|showstories|jobstories].json?print=pretty

It’s okay that if you build each API endpoint a standalone HoC (For single responsibility pattern or maybe due to business logic is vary). What if you can reuse the same HoC even for some combination too.

Below is an example, make it as dynamic as possible (Please prioritize maintaining rather than dynamic use case, as in most of the time the most dynamic function is the one hardest to maintain and debug, trade off)

import React, { Component } from "react";

export default function withStories(endpoint, dynamicProps) {
  return (WrappedComponent) => {
    return class extends Component {
      constructor(props) {
        super();

        this.state = {
          stories: []
        }
      }
    
      componentDidCatch() {
        // error handling
      }

      async componentDidMount() {
        try {
          const res = await fetch(endpoint, { method: "GET" }).then(res => res.json());

          if (res) {
            this.setState({ stories: res })
          }
        } catch (error) {
          // error handling
        }
      }

      render() {
        const { stories } = this.state;
        return <WrappedComponent 
                {...this.props} 
                {...{ [dynamicProps]: stories }} />
      }
    }
  }
}

Since, HoC is a JavaScript closures. We can modify it to take in and holding additional parameter – endpoint and dynamicProps.

This way a developer can passing any endpoint (Let’s limited to certain purpose or use case, otherwise it will caused the HoC hard to maintain at the end).

Then, the dynamicProps define how a developer accessing the the props. dynamicProps is defined by developer, for two purpose.

  • The business logic / HoC is encapsulated. Developer more focus on input and output. Less worry for handling business logic.
  • To prevent props collision, when it’s come to combine two or more HoC
export default function withStories(endpoint, dynamicProps) {
  return (WrappedComponent) => {
    return class extends React.Component {
      ...
      render() {
        return <WrappedComponent {...{ [dynamicProps]: value }} />
      }
    }
  }
}

Combines two or more HoC

One of the way, to combine multiple HoC as below:

withHoCOne(withHoCTwo(withHoCThree(App)));

Well, it solve the combine multiple HoC issue. At the same time, it create another issue – readability. Yes, the code is hard to read.

Here, recommend some utility to help solving the issue and maintain the code readability.

compose – from several package

This way we can combine multiple HoC without sacrifice the code readability.

compose(
  withHoCOne,
  withHoCTwo,
  withHoCThree,
)(App)

Beware of

Redundant props or some unused props. In ReactJS, a component is re-render when the props changed. So, if a HoC passing some redundant props and the value had changed it will caused the component to re-render. A bug that is hard to trace.

Conclusion

Wisely use of HoC is promoting the reuse of codebase. Improve maintainability and component unit testability.

Thanks for reading.  😁

Higher Order Component

Higher order Component in React Native – Part 1

tl;dr

Wrapped API in Higher order Component to promote reusable component and static component unit testing

Scenario

Create a component, fetching API/resources in componentDidMount. Most of the tutorial, start this way. This is the easier way for anyone to learn fetching API/resources in ReactJS/React Native. I don’t against this pattern. But, things go complicated in real world business. Fetching multiple API or reuse the same business logic (stateful business logic).

Copy paste might solve your problem and create duplicated code or even worse the codebase will grow very fast. What if you can done the job with a function instead of copy paste ? This way you can separate the business logic and presentation component, making component smaller and modular. It will be a good investment in long terms (reduce tech debt).

Wrapped API/resources fetching or business logic in Higher order Component promote the reusable of presentation component and business logic.

** A plain Higher order Component without too much dependencies allow you to share the same code with ReactJS for web or vice versa.

// Example

export default class App extends React.Components {
  componentDidMount() {
    fetch();
    fetch();
  }

  render() {
    return <View>
      {resources}
      {resources}
    </View>
  }
}

How to utilize Higher order Component (HoC) ?

*From here onward, will calling HackerNews API for demo purpose*

Let’s start with fetching topstories from HackerNews API – return a list of story ID. Then, fetching another API for particular story details.

// list of story ids
https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty

// details of story
https://hacker-news.firebaseio.com/v0/item/121003.json?print=pretty

First, let create the first Higher order Component (HoC), for topstories from HackerNews API.

import React, { Component } from "react";

export default function withTopStories(WrappedComponent) {
  return class extends Component {
    constructor(props) {
      super();

      this.state = {
        stories: []
      }
    }
    
    componentDidCatch() {
      // error handling
    }

    async componentDidMount() {
      try {
        const res = await fetch('https://hacker-news.firebaseio.com/v0/topstories.json?print=pretty', { method: "GET" }).then(res => res.json());

        if (res) {
          this.setState({ stories: res })
        }
      } catch (error) {
        // error handling
      }
    }

    render() {
      const { stories } = this.state;
      return <WrappedComponent {...this.props} stories={stories} />
    }
  }
}

In Higher order Component, component was parameterizedWrappedComponent. In above example, the HoC can now be reuse to any component that need to access the topstories resources.

The withTopStories will fetching the topstories resources from HackerNews API and set to the state – stories. Later on, passing the resources as a props to the parameterized component – WrappedComponent.

Accessing the injected props

Now, we want to access the injected props. Maybe a <FlatList />

import React, { Component } from "react";
import { FlatList, Text } from "react-native";
import withTopStories from "path/to/withTopStories";

class App extends Component {
  renderStories({ item, index }) {
    return <Text>{item}</Text>
  }

  render() {
    const { stories } = this.props;
    return <FlatList 
            data={stories} 
            renderItem={this.renderStories} 
            keyExtractor={(item, index) => `${item}-${index}`} 
           />
  }
}

export default withTopStories(App);

Then, we will render a list of number which is the story’s ID from HackerNews API. In order to get the details, we need the ID to fetch another API.

Yet another HoC for another endpoint

Then, we create another HoC for fetching the details of story by the ID.

import React, { Component } from "react";

export default function withStoryDetails(WrappedComponent) {
  return class extends Component {
    constructor(props) {
      super();

      this.state = {
        details: {}
      }
    }

    async componentDidMount() {
      try {
        const { storyID } = this.props;
        const details = await fetch(`https://hacker-news.firebaseio.com/v0/item/${storyID}.json?print=pretty`, { method: "GET" }).then(res => res.json());

        if (details) {
          this.setState({ details })
        }
      } catch (error) {
        // error handling
      }
    }

    render() {
      const { storyID, ...rest } = this.props;
      const { details } = this.state;
      return <WrappedComponent {...rest} details={details} />
    }
  }
}

Now, we need a component to display the details. Let’s create one. This time we won’t wrapped it directly, instead we will create a static version. This allow us easy to mock the data and unit testing the component.

import React from "react";
import { Text } from "react-native"

export default function Story(props) {
  const { details, ...rest } = props;
  return <Text {...rest}>{details.title}</Text>
}
/**
 * Version 2.0
 */
import React, { Component } from "react";
import { FlatList } from "react-native";
import withTopStories from "path/to/withTopStories";
import withStoryDetails from "path/to/withStoryDetails";
import Story from "path/to/Story"; // the component we create above

const WrappedStory = withStoryDetails(props => {
  const { storyID, ...rest } = props;
  return <Story storyID={storyID} {...rest} />
});

class App extends Component {
  renderStories({ item, index }) {
    // replace the below component 
    return <WrappedStory storyID={item} />
  }

  render() {
    const { stories } = this.props;
    return <FlatList 
             data={stories} 
             renderItem={this.renderStories} 
             keyExtractor={(item, index) => `${item}-${index}`} 
           />
  }
}

export default withTopStories(App);

Closing

Now, both React Native and ReactJS can share the HoC. Use it on mobile or web. Wrapped it with any presentation component without duplicate code. Easier to unit testing static component.

React Native 0.60

What’s new in React Native 0.60 ?

tl;dr


What’s new introduce in React Native 0.60 ?

  • Improvement on accessibility for both Android and iOS
  • Nested <View />within <Text /> components, but with limitation
  • touchSoundDisabled props introduce to <Button />, <Touchable />and <TouchableWithoutFeedback />for android only
  • Included a default eslintrc.js config file
  • Migration to AndroidX (Enabled by default)
  • Auto linking (Linking native modules)

  • New Introduction App Screen

Starting React Native 0.60 above modules will totally remove from React Native Core. In order to utilize specific module you need to install it as separate library from react-native-community.


Why you shouldn’t to React Native 0.60 immediately ?

Bug fixes and new features that introduce in React Native 0.60 is great. But, you may want to take consideration before upgrading.

The main issue is that React Native 0.60 migrate to AndroidX where most of the library has not ready yet for this changes. So, in order to upgrade to React Native 0.60. You may need to ensure all the third-party plugins/library in your application is compatible with AndroidX. Although, soon they will migrate to support AndroidX too. But, not at the moment.


Upgrade Helper

Upgrade helper – upgrade made easy. A new web tools that can help developer upgrading their react native easily. As it is comparing the different of two different react native version and show the difference side by side in web browser.

Google API for Android

tools:replace=”appComponentFactory”

tl;dr

17 June 2019 – Dear Google release new update for Google Play service and Firebase. Out of sudden, react native application failed at compile time and keep complaining the following message.

Manifest merger failed : Attribute [email protected] value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91
    is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).
    Suggestion: add 'tools:replace="android:appComponentFactory"' to <application> element at AndroidManifest.xml:5:5-19:19 to override.

But, why ?

Why is it related to Google releasing new update for Google Play service and Firebase ?

Google API for Android

Here is why. The update will required you to update your app to use Jetpack (AndroidX). Hence, you need to migrate your app to AndroidX, if you haven’t migrate yet. Android official website do provide the guide to do so – Migrating to AndroidX.

Adding the following to your gradle.properties file in the root project folder.
For react-native project – rootProject/android/gradle.properties

android.useAndroidX=true
android.enableJetifier=true

How about React Native ?

React Native plan to support AndroidX in the next release RN 0.60. React Native June Update. So, most likely that third-party React Native library will follow afterward.

If your react native app facing any issue as describe above, I suggest a few to solve your problem.

#1 If your app use any Google API service

In your build.gradle,

dependencies {
  // Please do this
  implementation "com.google.android.gms:play-services-gcm:16.1.0"

  // Not this, as this line will always fetching the latest version 
  // implementation "com.google.android.gms:play-services-gcm:+"
}

Locked the version of Google API service, instead to taking the latest version can save you from the error. As long as your version is not the one in latest version, then you are good to go.

#2 Updating third-party React Native library

So far, what I know is that react-native-device-info is releasing a patch to fixes this issue. Maybe there is other library affected as well, that you may need to find it out.

#3 Migrating to AndroidX

But, this is not a reliable way for React Native, especially if your app depend on a huge amount of third-party library.

For more details, please refer to this github issue.

Thanks for reading. Have a nice day.

Resources to learn programming

Resources to learn Programming and Certifications

tl;dr

A list of resources to learn programming in 2019.

#1 MOOC
#2 Programming Challenges/Contest
#3 Certifications

#1 MOOC (Massive Open Online Course)

  • Coursera – this is the first MOOC that I know
    • Where I learn about data structure and algorithms
    • Where you can find a lot of famous university offered their course here, Stanford, NTU (National Taiwan University), University of Michigan and more
  • edX
  • Udacity – Offered nanodegree
  • Udemy – Where you can buy or sell course individually
    • Currently, I’m taking the aws solutions architect course from here [June 2019]
  • taiwanmooc – Mooc in Taiwan, All in Chinese
  • OpenCourse by NTU(National Taiwan University)
  • Mooc China – All in Chinese

Coursera, edX or Udemy offered a wide range of courses. While, Udacity offered courses that are mostly related to technology.

Why taking programming class through MOOC is better than conventional university ?

  1. Offered updated technology in the industry.
    • Courses from local colleges or universities often outdated or not practical
  2. Offered both free and paid course, paid for the course only if you want a certificate from the course (Where the cost may vary, but cheaper than local colleges or universities)
  3. Get practical advise/skills from industry expert.
  4. Anyone have the chances to communicate or share your idea with others around the world

#2 Programming Challenges/Contest

#3 Certifications

1. IT Certifications

2. Cloud Certifications

Functional and Class Component

ReactJS – Functional and Class Components

tl;dr

ReactJS是一个由脸书(Facebook)在2013年所发布的UI library. ReactJS鼓吹以零件式(component-based) 的架构, 创建网站的前端. 可以想象成建构前端就像玩乐高(Lego)一样, 你所见到的按键(buttons), 链接(links), 页面(page), 输入(input), 截面(section), 段落(paragraph)或图像(images)等等, 都可以变成一个个独立的小零件, 然后在把它们进行组装. 以达到界面重用(Reusable UI component).

接下来, 是给想入手ReactJS的一些个人看法. 基本要求:
* HTML, CSS, JavaScript (ES 6)

#1 Component

Class Component

import React, { Component } from "react"

Class Sample extends Component {
  constructor(props) {
    super(props)

    this.state = {
      isActive: false
    }

    // 重点, 如果,不bind.当你click那个button,就会有error.
    // 不信, 你可以试试 😂
    this.handleToggleStatus.bind(this)
  }

  handleToggleStatus() {
    this.setState({
      isActive: !this.state.isActive
    })
  }

  render() {
    return (
      <>
        <button 
          onClick={this.handleToggleStatus} 
          style={{ backgroundColor: this.props.buttonColor || "#ffffff" }}
         >
           {this.state.isActive ? "Activated" : "Deactivated" }
        </button>

        // 这会是同样的一个按键,不过backgroundColor是红色的
        <Sample buttonColor={this.state.isActive ? "#ff0000" : "#ffffff" } />
      </>
    )
  }
}

如上所示, 每按一下按键(button), 字眼Activated和Deactivated就会跳动更换.

class component 也可以叫stateful component. 在class component里头你可以通过setState, 以更改component的state(状态). 每一个setState都会重新执行render(). 界面(UI)就会更新.

除了state, props的更新也会刷新界面(UI). 在上面的示范, 按键的backgroundColor会因为this.props.buttonColor的不同而改变.

Class component可以运用ReactJS的component’s lifecycle

一个component的state, 可以是其他component的props.

Tips 1: 为什么要bind ?
Tips 2: ReactJS 的Class/Stateful component的概念与Flutter的Stateful Widget是一样的.
Tips 3: <>...</>这个syntax是React Fragment的shorthand

Functional Component

import React from "react";

function Sample(props) {
  return <button {...props}>...</button>
}

在ReactJS 16.8版本之前, functional component又称Stateless component. 因为你只能通过props刷新界面(UI). 没有statesetState.

在ReactJS 16.8版本之后, 就有了React Hooks. React Hooks缩小了class component和functional component之间的差别. 简而言之, 你可以在functional component里头, 使用setState的概念. 如下,

import React, { useState } from "react"

function Sample(props) {
  // 顺序很重要, [value, function]
  // 第一个, 是value
  // 第二个, 是function
  const [isActive, setIsActive] = useState(false);

  function handleToggleStatus() {
    setIsActive(prevState => !prevState)
  }

  return (
    <button onClick={handleToggleStatus} >
      {isActive ? "Activated" : "Deactivated"}
    </button>
  )
}

See the Pen Functional Component and Hooks by Yik Kok (@yikkok-yong) on CodePen.

Tips 4: Functional/Stateless component的概念和Flutter的Stateless Widget是一样的.
Tips 5: Functional component里面不会需要this, 自然也不需要bind
Tips 6: React Hooks目前还未能完全取代class component

结语

Functional component 和 class component都有各自的好. 没有绝对. 视情况而使用. (只有更好, 没有最好)

File List View

Create a FileTree ListView in React Native

tl;dr

Demonstration of creating a file tree listview in React Native without third-party plugin. This demonstration is using latest release of React Native (0.59.5), React Hooks and functional components.

Personal feedback: Previously, I tend to use class components for creating components. But, I found out that functional components work better with Hot Reloading compare to class components. Now, with the help of React Hooks, making the functional components better. 🙂

  • FlatList
  • Native-base (Optional)
File List View

Main()

First of all, basically this is a listview using <FlatList />component from React Native. So, we can setup a basic listview component that composed <FlatList />.

import React, { memo, useState } from "react";
import { FlatList, Platform } from "react-native";

type Props = {
  data: Array<any>
};
function FileTree(props: Props) {
  const { data } = props;

  function renderNode({ item, index }) {
    return <FileTreeNode item={item} index={index} />;
  }

  // You can add others props to the <FlatList />
  // To optimize the performance
  return (
    <FlatList
      data={data}
      renderItem={renderNode}
      keyExtractor={keyExtractor}
    />
  );
}

// To keep every item in the list as unique as possible
// This might change accordingly to your data structure
function keyExtractor(item, index) {
  return `list-${item?.node?.label}-${index}`;
}

export default memo<Props>(FileTree);

So, this is the initial setup of our <FileTree />. No worry for any error or missing part.

Now, we can start implement the component <FileTreeNode />.

type NProps = {
  item: Array<any>,
  index: number
};
const FileTreeNode = memo<NProps>(function({ item, index }) {
  const [isOpen, setIsOpen] = useState(false);
  const label = item?.node?.label;
  const numberOfChildren = item?.children?.length || 0;
  const children = numberOfChildren > 0 ? item?.children : [];

  function handleOnPress(e) {
    setIsOpen(!isOpen);
  }

  const indentation = (item?.level + 1) * 20;
  return (
    <>
      <ListItem
        onPress={numberOfChildren > 0 ? handleOnPress : null}
        iconLeft
        style={{ marginLeft: indentation }}
      >
        <Left style={{ flex: 0 }}>
          {item?.node?.flag ? (
            <Thumbnail
              small
              source={item.node.flag}
              square
              resizeMethod="resize"
              resizeMode="contain"
            />
          ) : (
            <Icon name="pin" />
          )}
        </Left>
        <Body>
          <Text>{label}</Text>
        </Body>

        {numberOfChildren > 0 ? (
          <Right>
            {isOpen ? (
              <Icon name="keyboard-arrow-down" type="MaterialIcons" />
            ) : (
              <Icon name="keyboard-arrow-left" type="MaterialIcons" />
            )}
          </Right>
        ) : null}
      </ListItem>
      {isOpen && numberOfChildren > 0 ? <FileTree data={children} /> : null}
    </>
  );
});

<>...</> – act as a container to wrap the children component.

const [isOpen, setIsOpen] = useState(false);
const label = item?.node?.label;
const numberOfChildren = item?.children?.length || 0;
const children = numberOfChildren > 0 ? item?.children : [];

useState – React Hooks
?.Optional Chaining

Maybe you might wonder why item?.node?.label and item?.children ?Well, this is depend on how you structure your data. I structure it as follow.

export default [
  {
    node: {...detailsOfNode},
    level: 0, // for dynamically set the indentation margin
    children: [
      {
        node: {...detailsOfNode},
        level: 1,
        children: []
      }
    ]
  }
]
// any node will following the same pattern, including children

Anything inside the <ListItem /> is totally up to how you want to design layout and display what kind of information to user. I use the component from native-base to simplify the job. It can be replace by a <TouchableOpacity /> component.

Last but not least, we are going to reuse the <FileTree /> component to display our children from the data instead of writing a new <FlatList />

<FileTree data={children} />

End()

If you need a third-party plugin to accomplish the task. There is one available – react-native-nested-listview.

Thanks for reading and Happy Coding.  😁