Site icon Tutorial

React Basic Constituents

React is a JavaScript library aimed to create user interfaces and used extensively by social networking platforms like Facebook and Instagram to name a few.. It is fast, responsive, modular, we can create reusable files. We list its basic concepts.

Virtual DOM

JSX

import React from ‘react’;

import ReactDOM from ‘react-dom’;

ReactDOM.render(<h1>Hello</h1>, document.getElementById(‘app’));

ReactDOM.render(<h1>{2 + 3}</h1>, document.getElementById(‘app’));

const strings = [‘Jan’, ‘Feb’, ‘March’];

const listItems = strings.map(string => <li>{string}</li>);

const listItems = strings.map((string,i) => <li key={‘ string_’+i}>{string}</li>);

Component

import React from ‘react’; // This object contains the React library

import ReactDOM from ‘react-dom’;

class MyComponentClass extends React.Component {

render() {

return <h1>Hello world</h1>;

}

}

ReactDOM.render(<MyComponentClass/>,document.getElementById(‘app’));

import React from ‘react’;

export class NavBar extends React.Component {

render() {

return (

<h1>hello</h1>

);

}}

export can be done in the beginning as shown above or end using “export default className”

import React from ‘react’;

import ReactDOM from ‘react-dom’;

import { NavBar } from ‘./NavBar.js’;

class ProfilePage extends React.Component {

render() {

return (

<div><NavBar/></div>

);

}}

ReactDOM.render(<ProfilePage/>,document.getElementById(‘app’));

Props

import React from ‘react’;

import ReactDOM from ‘react-dom’;

class Greeting extends React.Component {

render() {

return <h1>My name is {this.props.firstName}!</h1>;

}}

ReactDOM.render(<Greeting firstName=’Thanos’ />, document.getElementById(‘app’));

import React from ‘react’;

export class Greeting extends React.Component {

render() {

return <h1>My name is {this.props.name}!</h1>;

}}

Lets import Greeting:

import React from ‘react’;

import ReactDOM from ‘react-dom’;

import {Greeting} from ‘./Greeting.js’;

class App extends React.Component {

render() {

return (

<div><Greeting name=”bahubali”/></div>

);

}}

ReactDOM.render(App />, document.getElementById(‘app’));

import React from ‘react’;

export class Button extends React.Component {

render() {

return (

<button onClick={this.props.talk}> me! </button>

);

}}

Lets import Button class:

import React from ‘react’;

import ReactDOM from ‘react-dom’;

import {Button} from ‘./Button’;

talk() {

alert(‘yaaayy!!!’);

}

class App extends React.Component {

render() {

return <Button talk={this.talk}/>;

}}

ReactDOM.render(Talker />, document.getElementById(‘app’));

import React from ‘react’;

class BigButton extends React.Component {

render() {

console.log(this.props.children);

return <button>THIS IS BIG BUTTON HERE</button>;

}}

// Example 1

<BigButton>

I am a child of BigButton.

</BigButton>

class Example extends React.Component {

render() {

return <h1>{this.props.text}</h1>;

}}

Example.defaultProps = { text: ‘yo’ };

State

class Example extends React.Component {

constructor(props) {

super(props);

this.state = { mood: ‘decent’ };

}

render() {

return <div></div>;

}}

class Example extends React.Component {

constructor(props) {

super(props);

this.pancake= this.pancake.bind(this);

}

pancake() {

this.setState({breakfast: ‘panake’});

}}

import React from ‘react’;

import ReactDOM from ‘react-dom’;

import { Child } from ‘./Child’

class Parent extends React.Component{

constructor(props) {

super(props);

this.state = { name: ‘Frarthur’ }

}

render() {

return <Child name={this.state.name}/>;

}}

ReactDOM.render(<Parent />, document.getElementById(‘app’));

Lets import to a stateless component:

import React from ‘react’;

export class Child extends React.Component {

render() {

return <h1>Hey, my name is {this.props.name}!</h1>;

}}

import React from ‘react’;

constructor(props) {

super(props);

this.handleChange=this.handleChange.bind(this);

}

// When a user selects a new dropdown item, it will invoke changeName, but it won’t pass the

// correct argument! Instead of passing a new name, it will pass an event object, as all event listeners // do. This is a common problem when passing down an event // handler in React! The solution is // to define another function that // can extract the name from the event object.

handleChange(e) {

const name = e.target.value;

this.props.onChange(name);

}

export class Child extends React.Component {

render() {

return (

<div>

<h1>Hey my name is {this.props.name}!h1>

<select id=”great-names” onChange={thi.handleChange }>

<option value=”Frarthur”>Amazonoption>

<option value=”Gromulus”>Google</option>

</div>

);

}}

When passing down event handler it will pass event object, so we need another function in child to extract the name from event object.

import React from ‘react’;

import ReactDOM from ‘react-dom’;

import { Child } from ‘./Child’;

class Parent extends React.Component {

constructor(props) {

super(props);

this.changeName = this.changeName.bind(this); // IMPORTANT

this.state = { name: ‘Frarthur’ };

}

changeName(newName) {

this.setState({name: newName});

}

render() {

return <Child name={this.state.name} onChange={this.changeName}/>

}}

ReactDOM.render(Parent />,document.getElementById(‘app’));

// Normal way to display component

export class Friend extends React.Component {

render() {

return <img src=’octopus.jpg’ />;

}

}

// Stateless functional component, we can also pass prop if required

export const Friend = () => {

return <img src=’octopus.jpg’ />;

}

Styles

Inline styles

<h1 style={{ color: ‘red’ }}>Hello world</h1>

Style as object variable:

const styles = {

color: ‘darkcyan’,

background: ‘mintcream’

};

<h1 style={styles}>Hello world</h1>

const styles = {

marginTop: ‘100px’,

fontSize: ‘50px’

};

Proptype

export class MessageDisplayer extends React.Component {

render() {

return <h1>{this.props.message}</h1>;

}

}

MessageDisplayer.propTypes = {

message: React.PropTypes.string

};

Forms

We all know forms from html. In react, for certain cases, it’s fine to have a form that is really just an input field. This is because, unlike in the traditional form paradigm, in React you re-send your form on every single character change. That removes the need to ever “submit” anything.

Lifecycle methods

 

Exit mobile version