Skip to content

Commit fa3fa23

Browse files
authored
React code snippets
1. How to create app 2. Fetch data from API 3. Routing example 4. Update npm packages 5. Make npm production build
1 parent 13c382d commit fa3fa23

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

ReactCodeSnippets.md

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# REACT CODE SNIPPETS
2+
# Create REACT APP
3+
```
4+
npx create-react-app my-app
5+
cd my-app
6+
npm start
7+
```
8+
# Fetch data from API REACT
9+
``` REACT JS
10+
import React, {Component} from 'react'
11+
12+
export default class StarWars extends Component {
13+
constructor(){
14+
super()
15+
this.state = {
16+
character: {},
17+
isLoading: false
18+
}
19+
}
20+
21+
componentDidMount(){
22+
this.setState({isLoading: true})
23+
fetch("https://swapi.co/api/people/1")
24+
.then(response => response.json())
25+
.then(data => (
26+
this.setState({
27+
isLoading: false,
28+
character: data
29+
})
30+
))
31+
}
32+
33+
34+
render(){
35+
const text = this.state.isLoading ? "People data is loading..." : this.state.character.name
36+
return(
37+
<div>
38+
<p>{text}</p>
39+
</div>
40+
)
41+
}
42+
}
43+
```
44+
# ROUTING
45+
```
46+
ReactDOM.render((
47+
48+
<Switch>
49+
50+
<Route exact path="/" component={Home} />
51+
52+
<Route path="/login" component={Login} />
53+
54+
<Route path="/explore" component={Explore} />
55+
56+
</Switch>),
57+
58+
document.getElementById('root')
59+
60+
);
61+
```
62+
# Update REACT Dependancies or Libraries
63+
```
64+
npm install -g npm-check-updates
65+
ncu -u
66+
npm update
67+
npm install
68+
```
69+
# npm run build
70+
```
71+
Builds the app for production to the build folder.
72+
It correctly bundles React in production mode and optimizes the build for the best performance.
73+
74+
The build is minified and the filenames include the hashes.
75+
76+
Your app is ready to be deployed.
77+
```

0 commit comments

Comments
 (0)