Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand the paradigm of React

I'm writing an article for my Uni about React and trying to understand what it's all about.

On there website the section Thinking in React, they point out that the UI should be broken up in small Components. One way of doing this is to use the technique of single responsibility principle.

Is this paradigm also the reason why HTML (JSX), CSS (inline styling) and JavaScript should be put in one file?

like image 785
Daniel Storch Avatar asked Jan 10 '16 10:01

Daniel Storch


People also ask

What paradigm is React?

React adheres to the declarative programming paradigm. Developers design views for each state of an application, and React updates and renders components when data changes. This is in contrast with imperative programming.

Why is React so hard to understand?

React is difficult to learn for beginners. This is due to its modular nature. Most React modules are interrelated and require you to use other software to build a complex application. You'll also need knowledge of functional programming.

How do you fix the syntax error that results from running this code?

How to Fix It: If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.


1 Answers

In a way including the HTML (JSX), CSS and JS in the same file is more about separation of concerns than single responsibility. The React component's concern is to render a small subset of information to the screen and the HTML (JSX), CSS and JS are all needed to do that, so therefore they are part of that concern.

Other schools of thought would consider those elements as different concerns, so this is very much an issue of how you interpret the pattern, rather than something with a definitive correct answer.

Pete Hunt talked specifically about Seperation of Concerns with React at JSConf EU in 2013, you can see a video of that here and read the slide deck from that presentation here . In addition to separation of concerns, that presentation also discusses how putting all the elements of a component in the same file can be said to increase cohesion and encourage loose coupling.

The practical upshot of including the HTML (JSX), CSS and JS in the same file is that you only need to look at one file to fully understand the behaviour of the component. That makes it easier to fully understand the component and aids reusability, especially by third-parties.

Facebook's Christopher Chedeau spoke about why they recommend putting CSS in with your React component at NationJS 2014. This is a video that talk and this is the slide deck.

like image 177
tomRedox Avatar answered Oct 19 '22 20:10

tomRedox