Demo Application

See how the News API can be used in an actual application.

We gained hands-on experience with the News API endpoints in the previous chapter. Now let's integrate those endpoints into a functional application.

Workflow

Our application can be used to search for different categories of headlines from several countries. Moreover, we can also search for articles available on the News API. The workflow of the application is as follows:

  1. When we run the application, the first page we see is the homepage. The homepage has two important sections:

    1. Navigation bar: This bar contains a drop-down menu to select our desired country.  It has navigation buttons for different news categories and a search button that leads to the search page.

    2. Mini section: Every mini section contains five top news articles from different categories.

  2. By clicking the "Read more" button on the top right of any mini section, we are taken to the designated page of the category for more articles. We can also access those designated pages by clicking the category in the navigator bar.

  3. The search page allows us to search for any article available on News API. The page also provides a filter box to make advanced searches.

Demo application

The widget below contains the code for our application. Press the "Run" button to see the React application in action.

# Getting Started with Create React App

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `npm start`

Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.

The page will reload when you make changes.\
You may also see any lint errors in the console.

### `npm test`

Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `npm run build`

Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `npm run eject`

**Note: this is a one-way operation. Once you `eject`, you can't go back!**

If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.

You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

### Code Splitting

This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)

### Analyzing the Bundle Size

This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)

### Making a Progressive Web App

This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)

### Advanced Configuration

This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)

### Deployment

This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)

### `npm run build` fails to minify

This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)
News application

Code explanation

Let’s dive into the code and see how we’ve integrated different News API endpoints into our React application. We will start by looking at the backend server for the application:

  • index.js is the “Node” server file that fetches responses from different News API endpoints by using the callApiAndSendResponseToClient and makeApiCall functions. It then sends the response to the front end of the application.

Our React-based front application contains several different components, each of which performs a specific function as explained below:

  • The Home component displays the homepage. The Home component uses the MiniSection component to display five top headlines for each category on the homepage. In line 97, we use the useFetch hook to make API calls to get data that is being rendered in different mini-sections of the homepage.

  • The Navbar component renders the navigation bar.

  • The Footer component is responsible for displaying the footer of the application at the bottom of the page.

  • The Section component displays the headlines for the selected category. This component is called when you click the "Read More" button on the top right of every mini-section on the homepage or when you click the category button such as “Business,” “General,” or “Health” in the navigation bar. In line 97, we use the useFetch hook to make an API call to get data for the section page.

  • The Search component renders the search page of the application. In line 42, we get our desired data by making the API call by using the customized endpoint URL.