Reactcheatsheetjs
Reactcheatsheetjs
JS
React.js
TABLE OF CONTENTS
Preface 1
Introduction 1
React.js Overview 1
A "Hello Word" Component 1
Component Specification 2
Using State And Properties 2
Using Context 4
Using AJAX 5
Component Styling 5
DOM References 6
Validating Properties 6
Custom Validation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Flux Application Architecture 7
Testing Components 8
React Addons 9
PREFACE
PREFACE creates a virtual representation of the DOM in
memory and updates only the parts that have
Welcome to the dynamic world of React.js, a changed in batches. This minimizes the number
cutting-edge JavaScript library that has of direct manipulations to the actual DOM,
revolutionized the landscape of front-end resulting in faster rendering.
development. In this cheatsheet, we embark on a
• Unidirectional Data Flow: React follows a
journey into the heart of React.js, exploring its core
unidirectional data flow, which means that
principles, capabilities, and the transformative
data flows in a single direction through the
impact it has had on building interactive and
components. This makes it easier to understand
efficient user interfaces.
and debug the application, as data changes are
predictable.
INTRODUCTION
INTRODUCTION
• JSX (JavaScript XML): React uses JSX, a syntax
This cheatsheet is designed to be a quick reference, extension for JavaScript that looks similar to
providing concise and essential information about XML or HTML. JSX allows you to write HTML-
the fundamental aspects of React.js, a JavaScript like code in your JavaScript files, making it
library for building user interfaces. Whether you’re more readable and expressive.
a beginner getting started with React or an • React Router: For building single-page
experienced developer looking for a quick applications with multiple views, React Router
reference, this cheatsheet is designed to help you is commonly used. It enables navigation among
navigate and understand React’s key concepts and views of different components, managing the
features. URL, and updating the UI accordingly.
<div>
<h1>Greetings</h1> // Create a context with a default
<HelloWorld message="Hello, theme
World!" /> const ThemeContext = createContext(
</div> 'light');
);
}; const HelloWorld = (props) => {
const [internalMessage,
export default App; setInternalMessage] = useState(
'Default Internal Message');
};
import PropTypes from 'prop-types';
export default MyComponent;
const MyComponent = ({ name, age,
DOMREFERENCES
REFERENCES isStudent }) => {
DOM
// Component logic
In React, accessing DOM elements directly is };
generally avoided, and the preferred approach is to
use React’s virtual DOM and state to manage MyComponent.propTypes = {
component rendering. However, there are name: PropTypes.string.isRequired,
situations where you may need to interact with the // String is required
actual DOM, such as focusing an input field, age: PropTypes.number,
measuring an element, or integrating with third- // Number is optional
party libraries that require direct DOM access.
isStudent: PropTypes.bool
.isRequired, // Boolean is required
React provides a feature called refs that can be used
to get a reference to a DOM element. Refs are
};
created using the useRef hook method.
MyComponent.defaultProps = {
age: 25,
import React, { useRef, useEffect } };
from 'react';
• PropTypes.instanceOf(MyClass): An instance of a
return null;
particular class.
};
• PropTypes.oneOf(['value1', 'value2']): A value
from a specified set.
• Actions: Actions represent events or user user interacts with the updated views, creating
interactions that trigger changes in the new actions and continuing the unidirectional
application state. They are simple objects flow.
containing a type property that describes the
action, and optionally, a payload with This architecture helps to maintain a clear and
additional data. predictable flow of data in the application, making
it easier to understand and debug. It’s important to
• Dispatcher: The Dispatcher is responsible for
note that Flux is a set of design principles, and there
distributing actions to registered stores. It is a
are various implementations and variations of Flux
central hub that manages the flow of data
in the wild, including popular libraries like Redux.
within the application. When an action is
dispatched, the Dispatcher notifies all
TESTINGCOMPONENTS
TESTING COMPONENTS
registered stores about the action. In other
words it is essentially an event system and
Testing React components can be done using testing
there can be only one global dispatcher.
libraries such as Jest and React Testing Library.
• Stores: Stores contain the application state and Let’s create a simple example of testing the "Hello
logic for handling actions. They respond to World" component using these libraries.
actions by updating their state and emitting
change events. Each store is responsible for a Firstly, you’ll need to install the necessary packages
specific domain or part of the application state. by running the following command:
A Store is a singleton and its the only entity in
the application that is aware of how to update npm install --save-dev jest @testing-
library/react @testing-library/jest-dom
data.
• Views (React Components): Views are React Now, let’s create a test file for our "Hello World"
components that display the application’s user component.
interface. They subscribe to changes in the
stores and update their presentation
accordingly. Views can trigger actions based on import React from 'react';
user interactions. import { render, screen, fireEvent }
from '@testing-library/react';
• Action Creators: Action Creators are utility
import '@testing-library/jest-
functions that encapsulate the logic for creating
actions. They are responsible for defining the dom/extend-expect'; // for
different types of actions that can occur in the additional matchers
application.
import HelloWorld from '
The data flow in Flux follows a unidirectional cycle: ./HelloWorld'; // Assuming your
component is in HelloWorld.js
• Action Creation: A user interacts with the
application, triggering the creation of an action
describe('HelloWorld Component', ()
by an Action Creator.
=> {
• Dispatch: The Action is dispatched to the test('renders the component with
Dispatcher, which forwards it to all registered
the provided props', () => {
stores.
const { container } = render(
• Store Update: Stores receive the action and <HelloWorld message="Testing Hello
update their state based on the action type. World" />);
They emit a change event to notify the views.
Addon Description
JCG delivers over 1 million pages each month to more than 700K software
developers, architects and decision makers. JCG offers something for everyone,
including news, tutorials, cheat sheets, research guides, feature articles, source code
and more.
CHEATSHEET FEEDBACK
WELCOME
support@javacodegeeks.com
Copyright © 2014 Exelixis Media P.C. All rights reserved. No part of this publication may be SPONSORSHIP
reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, OPPORTUNITIES
mechanical, photocopying, or otherwise, without prior written permission of the publisher. sales@javacodegeeks.com