Skip to content

Instantly share code, notes, and snippets.

@hackintoshrao
Created August 20, 2019 02:02
Show Gist options
  • Save hackintoshrao/71708a66757e1b3e839e6f4a05653cea to your computer and use it in GitHub Desktop.
Save hackintoshrao/71708a66757e1b3e839e6f4a05653cea to your computer and use it in GitHub Desktop.

React notes

Understanding the live tracking app by Hasura

Materials for React and GraphQL introduction

JSX

Generally, Markup for UI is embedded in HTML, logic in JS.

JSX combines both markup and logic. Here is the hello world of JSX.

const element = <h1>Hello, world!</h1>;

This syntax is neither a JS string nor HTML. It's JSX!

React uses JSX to create components. Components are the building blocks of any React app and a typical React app will have many of these. Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

Conceptually, components are like JavaScript functions. They accept arbitrary inputs (called “props”) and return React elements in JSX describing what should appear on the screen.

Reference to know more about docs.


At the heart of writing react application is a React component. A react component contains both logic and markup.

const element =

;

  • div is a DOM tag.
  • tabIndex is its attribute.
  • element is a React element.

An attribute of a DOM element could be an Javascript expression const element = ;

Components are user defined. And they return a react element.


How does a user define a component?

  • The simplest way to define a component is to write a JavaScript function:
function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

We will learn about props a bit later.

You can also use an ES6 class to define a component:

class Welcome extends React.Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}

The above two components are equivalent from React’s point of view.
You could see that both the components return a react element. 


Here, the name of the component is `Welcome`. 

A React component can be used inside a react element like this,

const element = <Welcome name="Sara" />;

This is used similar to how we used react elements with DOM tags.

const element = <div />;

And, a component can be rendered on web page using the ReactDOM.render() function.

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

const element = <Welcome name="Sara" />;
ReactDOM.render(
  element,
  document.getElementById('root')
);
1. We call ReactDOM.render() with the <Welcome name="Sara" /> element.
2. React calls the Welcome component with {name: 'Sara'} as the props.
3. Our Welcome component returns a <h1>Hello, Sara</h1> element as the result.
4. React DOM efficiently updates the DOM to match <h1>Hello, Sara</h1>.

props is optional to JSX component function. The following is a valid JSX function too.

function Welcome() {
  return <h1>Hello </h1>;
}

Components can refer to other components in their output

For example, we can create an App component that renders Welcome many times:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}

function App() {
  return (
    <div>
      <Welcome name="Sara" />
      <Welcome name="Cahal" />
      <Welcome name="Edite" />
    </div>
  );
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
);

Again, React components can just be Javascript functions which could return JSX.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment