| Start |
|---|
| What is ReactJS? ReactJS is an open-source, component based front end library responsible only for the view layer of the application. It is maintained by Facebook. ReactJS uses virtual DOM based mechanism to fill in data (views) in HTML DOM. The virtual DOM works fast owning to the fact that it only changes individual DOM elements instead of reloading complete DOM every time A React application is made up of multiple components, each responsible for outputting a small, reusable piece of HTML. Components can be nested within other components to allow complex applications to be built out of simple building blocks. A component may also maintain internal state - for example, a TabList component may store a variable corresponding to the currently open tab. React allows us to write components using a domain-specific language called JSX. JSX allows us to write our components using HTML, whilst mixing in JavaScript events. React will internally convert this into a virtual DOM, and will ultimately output our HTML for us. React "reacts" to state changes in your components quickly and automatically to rerender the components in the HTML DOM by utilizing the virtual DOM. The virtual DOM is an in-memory representation of an actual DOM. By doing most of the processing inside the virtual DOM rather than directly in the browser's DOM, React can act quickly and only add, update, and remove components which have changed since the last render cycle occurred. |
| Installation or Setup ReactJS is a JavaScript library contained in a single file Basic Inclusion To get the JavaScript files, go to the installation page (opens new window) of the official React documentation. React also supports JSX syntax (opens new window). JSX is an extension created by Facebook that adds XML syntax to JavaScript. In order to use JSX you need to include the Babel library and change Installing via npm You can also install React using npm (opens new window) by doing the following:
To use React in your JavaScript project, you can do the following: Installing via Yarn Facebook released its own package manager named Yarn (opens new window), which can also be used to install React. After installing Yarn you just need to run this command: You can then use React in your project in exactly the same way as if you had installed React via npm. |
| Hello World with Stateless Functions Stateless components are getting their philosophy from functional programming. Which implies that: A function returns all time the same thing exactly on what is given to it. For example:As you can see from the above example that, statelessSum is always will return the same values given a and b. However, statefulSum function will not return the same values given even no parameters. This type of function's behaviour is also called as a side-effect. Since, the component affects somethings beyond. So, it is advised to use stateless components more often, since they are side-effect free and will create the same behaviour always. That is what you want to be after in your apps because fluctuating state is the worst case scenario for a maintainable program. The most basic type of react component is one without state. React components that are pure functions of their props and do not require any internal state management can be written as simple JavaScript functions. These are said to be Here is a simple example to illustrate the concept of a Note that all that this component does is render an Since these components do not require a backing instance to manage the state, React has more room for optimizations. The implementation is clean, but as of yet no such optimizations for stateless components have been implemented (opens new window). |
Absolute Basics of Creating Reusable Components Components and PropsAs React concerns itself only with an application's view, the bulk of development in React will be the creation of components. A component represents a portion of the view of your application. "Props" are simply the attributes used on a JSX node (e.g. It can be useful to think of React components as simple functions - they take input in the form of "props", and produce output as markup. Many simple components take this a step further, making themselves "Pure Functions", meaning they do not issue side effects, and are idempotent (given a set of inputs, the component will always produce the same output). This goal can be formally enforced by actually creating components as functions, rather than "classes". There are three ways of creating a React component:
These components are used in exactly the same way: The above examples will all produce identical markup. Functional components cannot have "state" within them. So if your component needs to have a state, then go for class based components. Refer Creating Components (opens new window) for more information. As a final note, react props are immutable once they have been passed in, meaning they cannot be modified from within a component. If the parent of a component changes the value of a prop, React handles replacing the old props with the new, the component will rerender itself using the new values. See Thinking In React (opens new window) and Reusable Components (opens new window) for deeper dives into the relationship of props to components. |
| Create React App create-react-app (opens new window) is a React app boilerplate generator created by Facebook. It provides a development environment configured for ease-of-use with minimal setup, including:
InstallationFirst, install create-react-app globally with node package manager (npm). Then run the generator in your chosen directory. Navigate to the newly created directory and run the start script. Configurationcreate-react-app is intentionally non-configurable by default. If non-default usage is required, for example, to use a compiled CSS language such as Sass, then the eject command can be used. This allows editing of all configuration files. N.B. this is an irreversible process. AlternativesAlternative React boilerplates include:
Build React App To build your app for production ready, run following command |
| Hello World Without JSX Here's a basic example that uses React's main API to create a React element and the React DOM API to render the React element in the browser. With JSX Instead of creating a React element from strings one can use JSX (a Javascript extension created by Facebook for adding XML syntax to JavaScript), which allows to write as the equivalent (and easier to read for someone familiar with HTML) The code containing JSX needs to be enclosed in a So finally the above example becomes: |
| Hello World Component A React component can be defined as an ES6 class that extends the base A Component can also receive The example above shows how the component can render an arbitrary string passed into the A component can be rendered within any other component, or directly into the DOM if it's the topmost component, using By now you know how to make a basic component and accept For demo sake, let's make our Hello World app, display only the first name if a full name is given. Note: Each component can have it's own state or accept it's parent's state as a prop. Codepen Link to Example. (opens new window) RemarksReact (opens new window) is a declarative, component-based JavaScript library used for creating user interfaces. To achieve MVC framework like functionalities in React, developers use it in conjunction with a Flux (opens new window) flavour of choice, e.g. Redux (opens new window). |
Read Article
https://develop.horje.com/learn/1434/reference