The Most Hated React Code!

Programming with Mosh · Beginner ·🌐 Frontend Engineering ·2y ago

Key Takeaways

The video discusses the criticism of a React code snippet presented at the Next.js conference, addressing concerns such as SQL injection vulnerabilities, separation of concerns, and the use of server actions in Next.js. The speaker, Mosh, explains why these concerns are invalid and how Next.js is taking a great direction with server actions.

Full Transcript

this picture has been floating on the internet the past few days and a lot of people hate the code that is presented here so in this video I'm going to talk about some of the common arguments I will explain my point of view and at the end I will explain why I think the direction that react and Nyx are taking is in fact a great [Music] Direction my name is Mos and I've helped millions of people Advance their software engineering skills through this Channel and my online school code with.com so back to this picture to give you some context this picture is part of the nexj conference that was held last week now if you're are not familiar with nextjs it's basically a framework for building full stack applications with react on the front end and node on the back end so if you're a react developer you know that you can build the front end using react but to build the back end you have to use a separate backend framework like expressjs d Jango Ruby on Rails asp.net core and so on now with nextjs we can build both the front end and back end within the same project using the same language and the same set of tools now if you want to learn nextjs I have a couple of tutorials here on this channel and comprehensive courses on my website Cod with.com so what are the arguments against this code so here's the code that you saw in the slide here we have a react component called bookmark that renders a button with an icon and here we have a relatively new feature called a server action and that is this function you see here it's called a server action meaning it's an action or a function that gets executed on the server so when this button is clicked this piece of code is executed and as a result a bookmark is inserted into the database now a lot of people are arguing that this is like PHP but it took us 20 years to get back to where PHP was 20 years ago but this is not a valid argument argument because PHP runs entirely on the backend but here we have full power of front end and backend development now the second argument is that this code is vulnerable to SQL injection attacks a SQL injection attack is a kind of attack where a hacker can send SQL instructions to our application and do crazy things like inserting data deleting data and so on now this happens when we build SQL instructions dynamically using string concatenation so in this example we're building a SQL statement for inserting a bookmark into the bookmark table now because we're building this statement using string concatenation a hacker can send this input to our application and change our statement to something like this so in addition to inserting a bookmark they're also deleting all the users from the user table now the way we can prevent this is by using parameterized SQL statements so we Define our SQL statement with parameters which are placeholders for values what the user sends will be inserted into these placeholders so with this technique a hacker cannot change a SQL statement because it's fixed okay now the example that they were presenting at the next year's conference is using versel postgress package this page clearly explains that our SQL queries are constructed with the SQL template literal tag this function translates your query into a native postgress parameterized query to help prevent SQL injections so the argument that this code is vulnerable to SQL injection attacks is invalid now the third argument is that this code violates the separation of concerns principle but what is separation of concerns and why does it matter well separation of concerns is a fundamental design principle in software engineering it's suggests that by separating concerns we can make our code better organized more maintainable and potentially more reusable in this case we have some presentation code like the button and the icon mixed up with database related code so yes this code does violate the separation of concerns principle so to improve this the first thing we can do is take out this function outside of this component so I'm going to cut this code now let's define a function called add bookmark here we should give this a parameter called slug now back to our component here we pass an arrow function and call add bookmark so look the end result is cleaner and more maintainable now we could take this further and move this function at bookmark outside of this module and put it into a separate module but in this case I prefer to keep it here because realistically this is part of the implementation of this component so we are not going to get any extra value in terms of maintainability by moving these two lines of code into a separate file or module however if this component was more complex if it had hundreds of lines of code and if our database related code was more complicated yes then it would make sense to take this function and put it into a separate module the separation of concerns principle doesn't care how we should separate concerns it only suggests that by separating concerns we can make our code better organized more maintained and more reusable how we separate concerns is up to us and we have to decide on a caseby casee basis sometimes different functions within the same module sometimes different modules sometimes different folders sometimes different projects what I want you to take away from this video is that software engineering is not black and white you cannot take a principle and blindly apply it to solve every problem every problem is different so in this example I prefer to keep this function with in this module now the other argument is that we should not write SQL code in our components to start with and that is a fair argument the good thing is that server actions are not tied to this SQL function so here we can use an object relational mapper like Prisma and that's what I personally prefer as well so these were some of the common arguments now let me explain why I think the direction that nextjs is taking is in fact a great Direction so look before server actions if we wanted to implement this functionality first we would have to manually create an API endpoint for adding a bookmark then we would have to handle the click event of this button in our click Handler we would have to use the fetch API or a library like AXS to make an HTTP call to the back end most of the time this process is Trivial and very repetitive now let me take you a few steps back you know that when we call a function the control of execution moves from one place to another but to make this happen some magic has to happen under the hood that is invisible to us we don't have to worry about it these days right I learned this almost 20 years ago when I was coding in assembly now in most applications at some point the front end needs to talk to the back end so far we have been in charge of making that communication happen but nextjs is trying to free us from having to do so just like we don't have to think about how the control of execution moves from one place to another when we call a function so server actions allow the client code like this button to communicate with the back end without worrying about the transport without worrying about the HTTP protocol without worrying about creating API and points using the right HTTP verb like put post delete and without worrying about including the right data in the request server actions are going to take care of all that complexity so we can focus on solving real business problems so that was my take on this code if you enjoyed this video please like and share it with others and also make sure to subscribe to my channel for more videos like this

Original Description

This picture has been floating on the Internet these past few days and has received a lot of criticism from the community. This is part of the Next.js conference that was held last week. A lot of people argue that the code presented in this slide is violating many of the best practices we've learned. In this video, I'll explain my point of view on these arguments and share why I think the direction that React and Next.js are taking is a great direction. LEARN NEXT.JS Next.js Tutorial for Beginners: https://youtu.be/ZVnjOPwW4ZA?si=SeykjbciuytN1euA The Ultimate Next.js Course: https://codewithmosh.com/p/ultimate-nextjs-series MORE FROM MOSH Courses: https://codewithmosh.com Blog: https://programmingwithmosh.com Facebook: https://www.facebook.com/programmingwithmosh/ Twitter: https://twitter.com/moshhamedani #nextjs #reactjs #webdevelopment
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Programming with Mosh · Programming with Mosh · 0 of 60

← Previous Next →
1 6 Visual Studio Tips to Increase Your Productivity | Mosh
6 Visual Studio Tips to Increase Your Productivity | Mosh
Programming with Mosh
2 Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Visual Studio Keyboard Shortcuts that Speed Up Debugging Applications | Mosh
Programming with Mosh
3 Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Backbone.js Tutorial Part 2 - Backbone.js Models: Working with Model Attributes
Programming with Mosh
4 Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Backbone.js Tutorial Part 3 - Backbone.js Models: Model Validation
Programming with Mosh
5 Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Backbone.js Tutorial Part 4 - Backbone.js Models: Model Inheritance
Programming with Mosh
6 Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Backbone.js Tutorial Part 1 - Backbone.js Models: Creating Models
Programming with Mosh
7 Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Backbone.js Tutorial Part 5 - Backbone.js Models: Syncing Models with the Server
Programming with Mosh
8 Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Backbone.js Tutorial Part 6 - Backbone.js Collections: Creating Collections
Programming with Mosh
9 Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Backbone.js Tutorial Part 7 - Backbone.js Collections: Working with Collections
Programming with Mosh
10 Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Backbone.js Tutorial Part 8 - Backbone.js Collections: Fetching Collections from the Server
Programming with Mosh
11 Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Backbone.js Tutorial Part 9 - Backbone.js Views: Creating Views
Programming with Mosh
12 Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Backbone.js Tutorial Part 10 - Backbone.js Views: Passing Data to Views
Programming with Mosh
13 Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Backbone.js Tutorial Part 11 - Backbone.js Views: Handling the DOM Events
Programming with Mosh
14 Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Backbone.js Tutorial Part 12 - Backbone.js Views: Handling the Model Events
Programming with Mosh
15 Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Backbone.js Tutorial Part 13 - Backbone.js Views: Handling Collection Events
Programming with Mosh
16 Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Backbone.js Tutorial Part 14 - Backbone.js Views: Templating
Programming with Mosh
17 Clean Code: Learn to write clean, maintainable and robust code
Clean Code: Learn to write clean, maintainable and robust code
Programming with Mosh
18 C# Events and Delegates Made Simple | Mosh
C# Events and Delegates Made Simple | Mosh
Programming with Mosh
19 C# Generics Tutorial: Whats and Whys | Mosh
C# Generics Tutorial: Whats and Whys | Mosh
Programming with Mosh
20 Debugging C# Code in Visual Studio | Mosh
Debugging C# Code in Visual Studio | Mosh
Programming with Mosh
21 Repository Pattern with C# and Entity Framework, Done Right | Mosh
Repository Pattern with C# and Entity Framework, Done Right | Mosh
Programming with Mosh
22 Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Angular 2 Tutorial for Beginners: Learn Angular 2 from Scratch | Mosh
Programming with Mosh
23 Architecture of Angular 2+ Apps
Architecture of Angular 2+ Apps
Programming with Mosh
24 Working with Components in Angular
Working with Components in Angular
Programming with Mosh
25 C# Tutorial For Beginners - Learn C# Basics in 1 Hour
C# Tutorial For Beginners - Learn C# Basics in 1 Hour
Programming with Mosh
26 Difference between Junior and Senior Developers
Difference between Junior and Senior Developers
Programming with Mosh
27 Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Step-by-step ASP.NET MVC Tutorial for Beginners | Mosh
Programming with Mosh
28 [Pluralsight]: Become a Full-stack .NET Developer
[Pluralsight]: Become a Full-stack .NET Developer
Programming with Mosh
29 Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Xamarin Forms Tutorial: Build Native Mobile Apps with C#
Programming with Mosh
30 Value Types and Reference Types in JavaScript
Value Types and Reference Types in JavaScript
Programming with Mosh
31 Using Redux in Angular 2+ Apps | Mosh
Using Redux in Angular 2+ Apps | Mosh
Programming with Mosh
32 Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Testing Angular 2+ Apps with Jasmine and Karma | Mosh
Programming with Mosh
33 Profile and optimize your Angular 2 apps
Profile and optimize your Angular 2 apps
Programming with Mosh
34 Build a Real-world App with ASP.NET Core and Angular 2
Build a Real-world App with ASP.NET Core and Angular 2
Programming with Mosh
35 Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Entity Framework 6 Tutorial: Learn Entity Framework 6 from Scratch
Programming with Mosh
36 Two-way Binding and ngModel in Angular 4
Two-way Binding and ngModel in Angular 4
Programming with Mosh
37 Udemy Live 2017: Teaching Tech Panel
Udemy Live 2017: Teaching Tech Panel
Programming with Mosh
38 Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Demo of An E-commerce App Built with Angular, Firebase and Bootstrap 4
Programming with Mosh
39 My Brand New Angular Course
My Brand New Angular Course
Programming with Mosh
40 TypeScript Tutorial - TypeScript for React - Learn TypeScript
TypeScript Tutorial - TypeScript for React - Learn TypeScript
Programming with Mosh
41 Access Modifiers in TypeScript
Access Modifiers in TypeScript
Programming with Mosh
42 TypeScript Interfaces
TypeScript Interfaces
Programming with Mosh
43 TypeScript Classes
TypeScript Classes
Programming with Mosh
44 TypeScript Constructors
TypeScript Constructors
Programming with Mosh
45 TypeScript Properties
TypeScript Properties
Programming with Mosh
46 Angular Tutorial for Beginners: Learn Angular & TypeScript
Angular Tutorial for Beginners: Learn Angular & TypeScript
Programming with Mosh
47 AngularJS vs Angular 2 vs Angular 4 | Mosh
AngularJS vs Angular 2 vs Angular 4 | Mosh
Programming with Mosh
48 Angular Material Tutorial | Mosh
Angular Material Tutorial | Mosh
Programming with Mosh
49 Angular Animations Tutorial | Mosh
Angular Animations Tutorial | Mosh
Programming with Mosh
50 Firebase in Angular Applications | Mosh
Firebase in Angular Applications | Mosh
Programming with Mosh
51 Deploying Angular Applications | Mosh
Deploying Angular Applications | Mosh
Programming with Mosh
52 Building Forms in Angular Apps | Mosh
Building Forms in Angular Apps | Mosh
Programming with Mosh
53 Directives in Angular Applications
Directives in Angular Applications
Programming with Mosh
54 Routing and Navigation in Angular | Mosh
Routing and Navigation in Angular | Mosh
Programming with Mosh
55 Angular 4 in 40 Minutes
Angular 4 in 40 Minutes
Programming with Mosh
56 [NEW COURSE] Unit Testing for C# Developers
[NEW COURSE] Unit Testing for C# Developers
Programming with Mosh
57 Unit Testing C# Code - Tutorial for Beginners
Unit Testing C# Code - Tutorial for Beginners
Programming with Mosh
58 C# Classes Tutorial | Mosh
C# Classes Tutorial | Mosh
Programming with Mosh
59 C# Object Initializers Tutorial
C# Object Initializers Tutorial
Programming with Mosh
60 C# Constructors Tutorial | Mosh
C# Constructors Tutorial | Mosh
Programming with Mosh

The video discusses the criticism of a React code snippet and explains why the concerns are invalid, highlighting the benefits of using server actions in Next.js. The speaker provides examples and explanations to illustrate the concepts and principles discussed.

Key Takeaways
  1. Understand the context of the code snippet and the concerns raised
  2. Learn about server actions in Next.js and their benefits
  3. Apply software engineering principles to system design
  4. Use AI tools for development and apply AI-powered solutions to software engineering problems
💡 Server actions in Next.js allow for seamless communication between the frontend and backend, freeing developers from worrying about transport, HTTP protocol, and API endpoints, and enabling them to focus on solving real business problems.

Related Reads

📰
I Built a 100% Free, Frictionless Resume Builder with Direct PDF/Word Exports
Learn how to build a free and frictionless resume builder with direct PDF and Word exports, and discover the tools and techniques used to create it
Dev.to · Solangi Waqas
📰
How to Deploy Angular SSR on Cloudflare Workers for Free — No VPS, No Fees, No Credit Card.
Learn to deploy Angular SSR on Cloudflare Workers for free without needing a VPS, fees, or credit card
Medium · JavaScript
📰
How I Added Browser-Based HEIC to JPG Conversion (No Server, No Upload)
Learn how to convert HEIC to JPG in the browser without server upload using heic2any
Dev.to · Rushikesh Lade
📰
Angular Signals in 2026: From Fundamentals to Architect-Level Patterns
Learn Angular Signals fundamentals and architect-level patterns to improve your Angular applications
Dev.to · Amanulla Khan
Up next
Elementor Angie Ai Plugin Tutorial
Quick Tips - Web Desiign & Ai Tools
Watch →