Declarative Programming & React
Tags: javascript, react, programming, declarative
Published: 2018-12-26 12:14:30

---
title: Declarative Programming & React
published: true
description: 
tags: javascript, react, programming, declarative
cover_image: https://images.unsplash.com/photo-1531536241097-b5c69a099e79
canonical_url: https://itsjzt.com/posts/declarative-programming-react
---

*Cover Photo by [Mimi Thian](https://unsplash.com/photos/s2DbUV-yx2A?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/search/photos/coding?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)*

Declarative programming is something popularized by React in the JavaScript community. It is not a new thing but just got popularized in recent days.

According to Wikipedia:

> **declarative programming** is a programming paradigm — a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

This definition is nice one only if you know declarative programming but don't worry we will make sense out of it.

## Declarative Programming

Declarative programming is like describing the picture, where imperative programming is the instruction for painting that picture.  Declarative programming makes code:

- **more readable:** Program that are easier to read because it hides the lower level detail. In declarative programming we don't even know about the lower level details of the system.
- **easier to reason about:** the code we write is easier to reason about because it is much abstracted and we describe the solution instead of procedure.

As I said, declarative programming is not a new thing so there are many languages that are widely used in the industry that are declarative. Let me tell you a few of them

### SQL

SQL is a domain-specific language used in programming and designed for managing data held in a relational database. If you're reading this I don't think I need to give you any [introduction of SQL](https://en.wikipedia.org/wiki/SQL) it is just the de-facto standard for managing relational databases.

Look at this simple SQL query:  

```sql
SELECT * FROM Employees
```

Here we are defining we need every **Employees** detail not how to get the **Employees**. we aren't caring about any complex data structure the database is using to store the data.

### CSS

CSS is a nice example of declarative programming, In CSS we are actually defining how the element should look like and the browser takes care of implementing that for you. You can just say _this div should be blue in color and text should look bright yellow_ and the browser will do it for you.

Imagine if you set `body` to

```css
body {
  width: 500px;
  height: 500px;
  background: palevioletred;
  color: white;
}
```

now the browser makes the body according to your CSS. This is the concept of declarative programming, you define the structure and the compiler/host does it for you.

## Declarative programming in React

In react, You make interactive UIs by changing the state of the component and React takes care of updating the DOM according to it.

take this react code as an example:

```javascript
import React from "react";

class App extends React.Component {
  state = {
    message: "Hello react"
  }
  render() {
    return (
    <div className="App">
      <h1>{this.state.message}</h1>
      <button 
        onClick={e => this.setState({message: "Hello World"})}>
          Change Message</button>
    </div>
  );
  }
}
```

this creates a "hello React" message along with a button,  [which can you see here](https://codesandbox.io/s/j3v2p856py) like

![react-hello](https://thepracticaldev.s3.amazonaws.com/i/u5utlb7v3h9a4dmtvd3q.PNG)

When you click the button it changes the message to "Hello World". 

In react the DOM is declarative. This means we never interact with DOM, the UI is updated when we change the state. This makes it easier to design UI and debug them, You can just change the program's state and see how the UI will look at that particular time.



## TL;DR

In declarative programming, you describe the program/UI/picture and someone else React/Browser/OS implements it.