DBN

React Notes

Word count: 285Reading time: 1 min
2021/12/26

This post contains notes learned while setting up a tutorial React website.

Examples from Tutorial: Intro to React

Next up

Terms

Term Defination
props properties
Variable Scope Updatable
var global yes
let block yes
const block no

Loop over Array

Useful to create tables and lists.

1
2
3
4
5
return <div>
{user.map((person, index) => (
<h1>Hello, {<Welcome name={formatName(person)} />}</h1>
))}
</div>

Conditional Operator

1
condition ? true : false

Function as property

1
onClick={() => console.log('click')}

Functions

Simplier way to write compoents with only a render compoent and no state. Uses props.x format.

1
2
3
4
5
6
7
8
9
function Square(props) {
return (
<button className="square"
onClick={props.onSquareClick}
>
{props.squareValue}
</button>
)
}

Class

Uses this.props.x format.

1
2
3
4
5
6
7
8
9
class Board extends React.Component {
render() {
return (
<div>
{this.props.message}
</div>
);
}
}

Method (inside class)

1
2
3
4
5
6
7
8
renderSquare(i) {
return (
<Square
squareValue={this.props.squares[i]}
onSquareClick={() => this.props.onSquareClick(i)}
/>
);
}

Constructor (inside class)

Used to save state.

1
2
3
4
5
6
7
constructor(props) {
super(props)
this.state = {
stepNumber: 0,
xIsNext: true,
}
}

Use state inside event (inside class)

1
2
3
4
5
6
7
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this)
}
handleClick() {
blah
}

Or experimental

1
2
3
handleClick = () => {
console.log('this is:', this);
}

Arguments to events

1
2
<button onClick={(e) => this.deleteRow(id, e)}>Delete Row</button>
<button onClick={this.deleteRow.bind(this, id)}>Delete Row</button>

Author:Martin

Link:https://dbnservers.net/frontend/react-notes/

Publish date:December 26th 2021, 9:00:00 am

Update date:February 8th 2023, 3:30:42 am

License:creative commons 4.0

CATALOG
  1. 1. Terms
  2. 2. Loop over Array
  3. 3. Conditional Operator
  4. 4. Function as property
  5. 5. Functions
  6. 6. Class
  7. 7. Method (inside class)
  8. 8. Constructor (inside class)
  9. 9. Use state inside event (inside class)
  10. 10. Arguments to events