zhu-ting / Learning_React2018

This repo is for developers who want to learn the React library while learning the latest techniques currently emerging in the JavaScript language. This is an exciting time to be a JavaScript developer. The ecosystem is exploding with new tools, syntax, and best practices that promise to solve many of our development problems.
0 stars 0 forks source link

chapter03 Functional Programming with JavaScript #3

Open zhu-ting opened 6 years ago

zhu-ting commented 6 years ago

If you’ve mapped or reduced an array, then you’re already on your way to becoming a functional programmer. React, Flux, and Redux all fit within the functional JavaScript paradigm. Understanding the basic concepts of functional programming will elevate your knowledge of structuring React applications.

Functions can be sent to functions as arguments or returned from functions as results. More complex functions, called higher-order functions, can manipulate functions and use them as either arguments or results or both.

We are going to go over some of the key concepts of functional pro‐ gramming, and we’ll cover how to implement functional techniques with JavaScript.

What It Means to Be Functional

JavaScript supports functional programming because JavaScript functions are first- class citizens. This means that functions can do the same things that variables can do. ES6 adds language improvements that can beef up your functional programming techniques, including arrow functions, promises, and the spread operator

zhu-ting commented 6 years ago

Imperative Versus Declarative.

var string = "This is the midday show with Cheryl Waters"; var urlFriendly = "";
for (var i=0; i<string.length; i++) { if (string[i] === " ") {
       urlFriendly += "-";
 }else{
        urlFriendly += string[i];
      }
    }
 console.log(urlFriendly);
const string = "This is the mid day show with Cheryl Waters" 
const urlFriendly = string.replace(/ /g, "-")
console.log(urlFriendly)
zhu-ting commented 6 years ago

Let’s consider the task of building a document object model, or DOM. An imperative approach would be concerned with how the DOM is constructed:

var target = document.getElementById('target'); 
var wrapper = document.createElement('div'); 
var headline = document.createElement('h1');
    wrapper.id = "welcome";
    headline.innerText = "Hello World";
    wrapper.appendChild(headline);
    target.appendChild(wrapper);
zhu-ting commented 6 years ago

let’s take a look at how we can construct a DOM declaratively using a React component:

const { render } = ReactDOM
const Welcome = () => (
  <div id="welcome">
     <h1>Hello World</h1>
  </div>
)
    render(
        <Welcome />,
        document.getElementById('target')
    )

React is declarative. Here, the Welcome component describes the DOM that should be rendered. The render function uses the instructions declared in the component to build the DOM, abstracting away the details of how the DOM is to be rendered. We can clearly see that we want to render our Welcome component into the element with the ID of 'target'.

zhu-ting commented 6 years ago

Functional Concepts