On the very first page of reactjs.org, it says that React
is declarative, which is a major property of Functional codes.
Before talking about why functional programming is important when building a project with React
, I want to talk about some basic concepts of functional programming.
In order to understand the definition of a pure function, you need to understand what side effects are. A side effect is anything that is observable other than the return value of the function such as console.log()
as it allows you to observe a state change even when the value is not the return value of a function.
Share states could be helpful if you need to pass values to different components—like props
in React. This is dangerous especially when the order of function execution is critical. If function B gets called before function A when a variable in function B must be updated via function A before its execution, it would lead you to a bug.
By avoiding shared state, the timing and order of function calls do not change the result of calling the function
To be honest, I do not completely understand why this is important with Functional Programming, but apparently the data flow of your project gets messy. I believe this is related to how a React state
must be treated as immutable. And a React prop
is literally immutable. This means that you cannot directly change a state in a React project without using setState
.
A higher order function is a function whith takes a function as an argument and returns a function, or both the argument and a function.