Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 8704

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 60k
Author
Asked: November 28, 20242024-11-28T03:36:09+00:00 2024-11-28T03:36:09+00:00

Day 6: Meeting React

  • 60k

The Feynman technique says that teaching a subject makes you better at it, which is what I'm trying to do here. You may correct me if you saw mistakes in this post

JSX and React

Writing HTML inside a JS string is a pain in the ass, so JSX is introduced to make this easier for developers to write HTML in JS code:

let elem = <h1>Yeet</h1> // HTML made easy in JS! 🚀 
Enter fullscreen mode Exit fullscreen mode

However, JSX violates the JS syntax, so you need to compile them into vanilla JS first. Babel transpiler is a popular option for this task.

Note that JSX syntax only works with one element at a time. You can nest more elements inside, but not next to each other.

And for commenting, you use {* comment *} syntax.

There are a few special cases where the HTML syntax differs in JSX. An important one is class:

let container = <div className="Nope"></div>; 
Enter fullscreen mode Exit fullscreen mode

In fact, all HTML attributes in JSX follow camelCase 🐪 naming, so onclick becomes onClick, and such.

Another important point is that JSX allows all elements to be self-closing, but requires all element to close itself.

let abnormal = <div /> // I'm self closing! let closed = <br /> // I need to be closed! 
Enter fullscreen mode Exit fullscreen mode

Finally, you can also insert JS code inside the syntax:

let username = "Bob Ross"; let title = <h1>My name is {username}</h1>; 
Enter fullscreen mode Exit fullscreen mode

All about React

React is a JS library for building UI, so it has the necessary power to manipulate DOM to our liking.

To render elements into DOM, use ReactDOM.render() method:

let box = document.getElementById("section1"); let content = <h1>Cool!</h1>;  ReactDOM.render(content, box); 
Enter fullscreen mode Exit fullscreen mode

React works with the concept of components. Basically, React encourages developers to break the website into smaller functional pieces. This makes it easy to separate UI code from functional code.

There are two approaches to writing components in React:

  1. Stateless functional component

This approach use function in JS to write stateless components (I'm still learning about states, but what can I say is that stateless component can receive and render data, but not track or manage data.)

const Recipe = (props) => {   return <ul>            <li>Flour</li>            <li>Egg</li>            <li>Love</li>          </ul> } 
Enter fullscreen mode Exit fullscreen mode

They must have capitalized name, and only return JSX or null. props is a standard name for its argument, and arrow function syntax is very common practice.

  1. ES6 class syntax

Here's the syntax:

class Drinks extends React.Component {   constructor(props) {     super(props);   }   render() {     return (       <h1>Coffee, Tea, and more!</h1>     );   } }; 
Enter fullscreen mode Exit fullscreen mode

This may looks intimidating, but the only important thing for now is the render() function. The rest is boilerplate most of the time. Just like the stateless functional component, you must have a capitalized name.

The Drinks class inherits the React.Component class. The constructor() function is responsible to initialize the class. The super() functions means that it borrows the constructor function from its parent, which is React.Component. The render() function is called for DOM rendering.

To draw this component into DOM, we still use ReactDOM.render() to do it, but with a different syntax:

let target = document.body; ReactDOM.render(<Drinks />, target); 
Enter fullscreen mode Exit fullscreen mode

The components can be used as a custom HTML tag in JSX, which would be useful for the next part.

Mix-and-match elements

As shown before, you can use components to make custom HTML tag in JSX. These tags would show whatever the render() function of the component returns.

Also, as HTML tags, they can be nested 🤯!

class Drinks extends React.Component {   constructor(props) {     super(props);   }   render() {     return (       <h1>Coffee, Tea, and more!</h1>     );   } };  const Menu = (props) => {   return (     <div>       <Drinks />       <p>Lorem Ipsum Dolor</p>     </div> ); 
Enter fullscreen mode Exit fullscreen mode

Note that this custom HTML tags behave like others, so they are very flexible.

Afterwords

Woohoo, I got a lot done today. This is a very good progress for me, and I'm proud of it. I hope others who are going through this to share my enthusiasm, and to keep going through it.

Follow me on Github!
Also on Twitter!

100daysofcodebeginnersreactwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.