ClickCease

React.js Programming

1. Basics

Creating a New React App:


npx create-react-app my-app
cd my-app
npm start  # Start the development server

2. Component Creation

Functional Component:


import React from 'react';
const MyComponent = () => {
  return <div>Hello, React!</div>;
};
export default MyComponent;
Class Component:

import React, { Component } from 'react';
class MyComponent extends Component {
  render() {
    return <div>Hello, React!</div>;
  }
}
export default MyComponent;

3. JSX

Using JSX:


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

const name = 'React';
const element = <h1>Hello, {name}!</h1>;

4. Props

Passing Props:


const MyComponent = ({ title }) => <h1>{title}</h1>;
<MyComponent title="Welcome to React!" />

5. State

Using State in Functional Components (Hooks):


import React, { useState } from 'react';
const MyComponent = () => {
  const [count, setCount] = useState(0);
   return (
    <div>
      <p>You clicked {count} times</p>
      <button onClick={() => setCount(count + 1)}>Click me</button>
    </div>
  );
};
Using State in Class Components:

class MyComponent extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
  render() {
    return (
      <div>
        <p>You clicked {this.state.count} times</p>
        <button onClick={() => this.setState({ count: this.state.count + 1 })}>
          Click me
        </button>
      </div>
    );
  }
}

6. Effects

Using useEffect:


import React, { useEffect } from 'react';
const MyComponent = () => {
  useEffect(() => {
    document.title = 'Hello, React!';
  }, []); // Empty array for componentDidMount
  return <div>Hello, React!</div>;
};
Handling 

7. Handling Events

Event Handling:


const handleClick = () => {
  alert('Button clicked!');
};
<button onClick={handleClick}>Click me</button>

8. Conditional Rendering

Using Conditional Rendering:


const MyComponent = ({ isLoggedIn }) => (
  <div>
    {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign in.</h1>}
  </div>
);

9. Lists

Rendering Lists:


const items = ['Apple', 'Banana', 'Cherry'];
const List = () => (
  <ul>
    {items.map((item, index) => (
      <li key={index}>{item}</li>
    ))}
  </ul>
);

10. Forms

Handling Forms:


const MyForm = () => {
  const [value, setValue] = useState('');
  const handleSubmit = (e) => {
    e.preventDefault();
    console.log(value);
  };
  return (
    <form onSubmit={handleSubmit}>
      <input
        type="text"
        value={value}
        onChange={(e) => setValue(e.target.value)}
      />
      <button type="submit">Submit</button>
    </form>
  );
};

11. Context API

Creating Context:


const MyContext = React.createContext();
const MyProvider = ({ children }) => {
  const [value, setValue] = useState('default');
  return (
    <MyContext.Provider value={{ value, setValue }}>
      {children}
    </MyContext.Provider>
  );
};

12. React Router

Setting Up Routing:


npm install react-router-dom
Basic Routing Example:

import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
const App = () => (
  <Router>
    <Switch>
      <Route path="/" exact component={Home} />
      <Route path="/about" component={About} />
    </Switch>
  </Router>
);

13. Common Commands


npm start                   # Start the development server
npm run build               # Build for production
npm test                    # Run tests
Download Elysium Spark Note

Facebook
X
LinkedIn
Pinterest
WhatsApp