Convert Figma logo to code with AI

reactjs logoreact-tutorial

Code from the React tutorial.

3,290
2,133
3,290
6

Top Related Projects

227,213

The library for web and native user interfaces.

207,677

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

95,657

Deliver web apps with confidence 🚀

78,194

Cybernetically enhanced web apps

36,546

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

27,910

A rugged, minimal framework for composing JavaScript behavior in your markup.

Quick Overview

The reactjs/react-tutorial repository is an official tutorial project for React, maintained by the React team. It guides developers through building a simple tic-tac-toe game using React, demonstrating core concepts and best practices for React development.

Pros

  • Provides a hands-on, practical introduction to React
  • Maintained by the official React team, ensuring accuracy and relevance
  • Covers fundamental React concepts in a clear, step-by-step manner
  • Includes a working example that developers can build upon

Cons

  • May be too basic for experienced React developers
  • Focuses on a single, simple application, which might not cover all real-world scenarios
  • Could benefit from more frequent updates to keep pace with React's evolution
  • Lacks coverage of more advanced React topics and ecosystem tools

Code Examples

// Creating a functional component
function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

This code demonstrates how to create a functional component in React, representing a square in the tic-tac-toe game.

// Using state in a class component
class Board extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      squares: Array(9).fill(null),
      xIsNext: true,
    };
  }

  handleClick(i) {
    const squares = this.state.squares.slice();
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }

  // ... render method and other code
}

This example shows how to manage state in a class component, including initializing state in the constructor and updating it in response to user actions.

// Lifting state up
class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [{
        squares: Array(9).fill(null),
      }],
      stepNumber: 0,
      xIsNext: true,
    };
  }

  // ... other methods

  render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    return (
      <div className="game">
        <div className="game-board">
          <Board
            squares={current.squares}
            onClick={(i) => this.handleClick(i)}
          />
        </div>
        {/* ... other JSX */}
      </div>
    );
  }
}

This code demonstrates the concept of lifting state up by moving the game's state to the top-level Game component and passing it down to child components as props.

Getting Started

To get started with the React tutorial:

  1. Clone the repository:

    git clone https://github.com/reactjs/react-tutorial.git
    
  2. Navigate to the project directory:

    cd react-tutorial
    
  3. Install dependencies:

    npm install
    
  4. Start the development server:

    npm start
    
  5. Open your browser and visit http://localhost:3000 to see the application running.

Follow the tutorial instructions in the repository's README to build the tic-tac-toe game step by step.

Competitor Comparisons

227,213

The library for web and native user interfaces.

Pros of React

  • Comprehensive and actively maintained official repository
  • Extensive documentation and resources for all aspects of React development
  • Large community and ecosystem with numerous third-party libraries and tools

Cons of React

  • Steeper learning curve for beginners due to its extensive features and concepts
  • Larger codebase and more complex structure compared to the tutorial repository
  • May be overwhelming for those seeking a simple introduction to React

Code Comparison

React (simplified example):

import React from 'react';
import ReactDOM from 'react-dom';

function App() {
  return <h1>Hello, world!</h1>;
}

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

React Tutorial:

class Square extends React.Component {
  render() {
    return (
      <button className="square" onClick={() => alert('click')}>
        {this.props.value}
      </button>
    );
  }
}

The React repository contains the full source code and documentation for the React library, while the React Tutorial repository focuses on a specific example (tic-tac-toe game) to teach React basics. The React codebase is more complex and feature-rich, whereas the tutorial code is simpler and more focused on learning fundamentals.

207,677

This is the repo for Vue 2. For Vue 3, go to https://github.com/vuejs/core

Pros of Vue

  • More comprehensive documentation and examples in the repository
  • Simpler syntax and lower learning curve for beginners
  • Built-in state management solution (Vuex) included in the ecosystem

Cons of Vue

  • Smaller community and ecosystem compared to React
  • Fewer job opportunities and enterprise adoption
  • Less flexibility for large-scale applications

Code Comparison

Vue component:

<template>
  <div>{{ message }}</div>
</template>

<script>
export default {
  data() {
    return {
      message: 'Hello Vue!'
    }
  }
}
</script>

React component:

import React from 'react';

function HelloMessage({ message }) {
  return <div>{message}</div>;
}

export default HelloMessage;

The Vue example showcases its single-file component structure, combining template, script, and style (not shown) in one file. The React example demonstrates its more JavaScript-centric approach, using JSX for rendering.

Both repositories serve as learning resources, but Vue provides a more structured tutorial experience within its repository. React-tutorial is more focused on a specific example application, while Vue's repository contains comprehensive documentation and guides for various aspects of the framework.

95,657

Deliver web apps with confidence 🚀

Pros of Angular

  • Full-featured framework with built-in tools for routing, forms, and HTTP requests
  • Typescript support out of the box, providing better type checking and tooling
  • Comprehensive documentation and extensive ecosystem

Cons of Angular

  • Steeper learning curve due to its complexity and numerous concepts
  • Larger bundle size, potentially impacting initial load times
  • More opinionated, which may limit flexibility in some cases

Code Comparison

Angular component:

@Component({
  selector: 'app-root',
  template: `<h1>{{title}}</h1>`
})
export class AppComponent {
  title = 'Hello, Angular!';
}

React component:

function App() {
  return (
    <h1>Hello, React!</h1>
  );
}

Angular uses a decorator-based approach with TypeScript, while React uses a more functional style with JSX. Angular's template syntax is separate from the component logic, whereas React combines them in JSX.

Angular's repository is more comprehensive, containing the entire framework, while the React tutorial repository is focused on a specific learning example. This makes direct comparisons challenging, as they serve different purposes. Angular provides a complete solution for building large-scale applications, while the React tutorial offers a simpler introduction to React's core concepts.

78,194

Cybernetically enhanced web apps

Pros of Svelte

  • Smaller bundle size and better performance due to compile-time optimization
  • Simpler syntax with less boilerplate code
  • Built-in state management without additional libraries

Cons of Svelte

  • Smaller ecosystem and community compared to React
  • Fewer job opportunities and resources for learning
  • Limited tooling and integration options

Code Comparison

React:

function Square(props) {
  return (
    <button className="square" onClick={props.onClick}>
      {props.value}
    </button>
  );
}

Svelte:

<script>
  export let value;
  export let onClick;
</script>

<button class="square" on:click={onClick}>
  {value}
</button>

The Svelte example demonstrates its more concise syntax, with less boilerplate and a more intuitive component structure. React, on the other hand, uses JSX and requires explicit function declarations for components.

Both frameworks offer powerful ways to build user interfaces, but Svelte's approach focuses on simplicity and performance, while React provides a more established ecosystem and wider adoption in the industry.

36,546

⚛️ Fast 3kB React alternative with the same modern API. Components & Virtual DOM.

Pros of Preact

  • Smaller bundle size and faster performance
  • API compatibility with React, allowing easy migration
  • Built-in support for class and functional components

Cons of Preact

  • Smaller ecosystem and community compared to React
  • Less extensive documentation and learning resources
  • Some advanced React features may not be available or require additional setup

Code Comparison

React:

import React from 'react';
import ReactDOM from 'react-dom';

function App() {
  return <h1>Hello, world!</h1>;
}

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

Preact:

import { h, render } from 'preact';

function App() {
  return <h1>Hello, world!</h1>;
}

render(<App />, document.body);

Key Differences

  • Preact uses h instead of React.createElement
  • Preact's render function is imported directly, while React uses ReactDOM.render
  • Preact can render directly to document.body, while React typically uses a root element

Summary

Preact offers a lightweight alternative to React with similar syntax and functionality. It's ideal for projects where performance and bundle size are critical. However, React's larger ecosystem and more extensive documentation may be preferable for complex applications or teams with existing React expertise.

27,910

A rugged, minimal framework for composing JavaScript behavior in your markup.

Pros of Alpine

  • Lightweight and minimal, with a smaller learning curve
  • No build step required, can be used directly in HTML
  • Easier to integrate into existing projects

Cons of Alpine

  • Less powerful for complex applications compared to React
  • Smaller ecosystem and community support
  • Limited state management capabilities for large-scale apps

Code Comparison

Alpine:

<div x-data="{ count: 0 }">
  <button @click="count++">Increment</button>
  <span x-text="count"></span>
</div>

React:

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <div>
      <button onClick={() => setCount(count + 1)}>Increment</button>
      <span>{count}</span>
    </div>
  );
}

Summary

Alpine is a lightweight JavaScript framework that aims to simplify front-end development by providing a minimal set of directives and utilities. It's easy to learn and integrate into existing projects without a build step. React, on the other hand, is a more powerful and flexible library with a larger ecosystem, better suited for complex applications. Alpine shines in simplicity and ease of use, while React offers more robust features for large-scale development. The choice between the two depends on project requirements, team expertise, and scalability needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Deploy

React Tutorial

This is the React comment box example from the React tutorial.

To use

There are several simple server implementations included. They all serve static files from public/ and handle requests to /api/comments to fetch or add data. Start a server with one of the following:

Node

npm install
node server.js

Python

pip install -r requirements.txt
python server.py

Ruby

ruby server.rb

PHP

php server.php

Go

go run server.go

Perl

cpan Mojolicious
perl server.pl

And visit http://localhost:3000/. Try opening multiple tabs!

Changing the port

You can change the port number by setting the $PORT environment variable before invoking any of the scripts above, e.g.,

PORT=3001 node server.js