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:
import React, { Component } from 'react'; class MyComponent extends 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> ); } } export default MyComponent;
6. Effects
Using useEffect:
import React, { useEffect } from 'react'; const MyComponent = () => { useEffect(() => { document.title = 'Hello, React!'; }, []); // Runs only once (componentDidMount) return <div>Hello, React!</div>; }; export default MyComponent;
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:
import React, { useState } from 'react'; 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> ); }; export default MyForm;
11. Context API
Creating Context:
import React, { useState } from 'react'; const MyContext = React.createContext(); const MyProvider = ({ children }) => { const [value, setValue] = useState('default'); return ( <MyContext.Provider value={{ value, setValue }}> {children} </MyContext.Provider> ); }; export { MyContext, MyProvider };
12. React Router
Setting Up Routing:
npm install react-router-dom
Basic Routing Example:
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; import Home from './Home'; import About from './About'; const App = () => ( <Router> <Switch> <Route path="/" exact component={Home} /> <Route path="/about" component={About} /> </Switch> </Router> ); export default App;
13. Common Commands
npm start # Start the development server npm run build # Build for production npm test # Run tests
This Elysium Spark Note will help you navigate React development efficiently!