Start with an REACT APP
Follow the facebook official react page:
https://github.com/facebookincubator/create-react-app
import css File to the App
First you need to do
|
|
In some cases you will face an error which is
|
|
The easier way to fix this problem is to move GithubPages(or the folder in your error) to another place and then run
|
|
Then try npm run eject
again. That should work. :D
After ejecting, we need to mofidy two files.
./config/webpack.config.dev.js
search for .css, and find the following block:
123456789test: /\.css$/,use: [require.resolve('style-loader'),{loader: require.resolve('css-loader'),options: {importLoaders: 1,},},modify it to
1234567891011test: /\.css$/,use: [require.resolve('style-loader'),{loader: require.resolve('css-loader'),options: {importLoaders: 1,modules: true,localIdentName: '[name]__[local]__[hash:base64:5]'},},
./config/webpack.config.prod.js
still search for .css, and find:
12345678910111213141516171819test: /\.css$/,loader: ExtractTextPlugin.extract(Object.assign({fallback: {loader: require.resolve('style-loader'),options: {hmr: false,},},use: [{loader: require.resolve('css-loader'),options: {importLoaders: 1,minimize: true,sourceMap: shouldUseSourceMap,},},and modify use to
1234567891011use: [{loader: require.resolve('css-loader'),options: {importLoaders: 1,minimize: true,sourceMap: shouldUseSourceMap,modules: true,localIdentName: '[name]__[local]__[hash:base64:5]'},},The in the js file, you can use:
1234import classes from './test.css';//jsx, for the div<div className={classes.test}></div>
React Routing Tips
https://react-guide.github.io/react-router-cn/
You need to install two modules first.
|
|
Writing a link:
|
|
JS:
|
|
Some other useful components of react-router-dom are:
NavLink, Link, withRouter, Switch(Load the first matching, and order is important), Redirect
|
|
Query Params:
You can pass them easily like this:
<Link to="/my-path?start=5">Go to Start</Link>
or
|
|
React router makes it easy to get access to the search string: props.location.search
.
But that will only give you something like ?start=5
You probably want to get the key-value pair, without the ?
and the =
. Here’s a snippet which allows you to easily extract that information:
|
|
URLSearchParams
is a built-in object, shipping with vanilla JavaScript. It returns an object, which exposes the entries()
method. entries()
returns an Iterator - basically a construct which can be used in a for...of...
loop (as shown above).
When looping through query.entries()
, you get arrays where the first element is the key name (e.g. start
) and the second element is the assigned value (e.g. 5
).
Fragment:
You can pass it easily like this:
<Link to="/my-path#start-position">Go to Start</Link>
or
|
|
React router makes it easy to extract the fragment. You can simply access props.location.hash
.
Navigate Programmatically
|
|
Handling the 404 Error
Under all route components, write:
|
|
You can also use the component instead of render as well.
Lazy loading
Adding /Hoc/asyncComponent/asyncComponent.js
, and write the following code:
|
|
How to use this file?
|
|
Using Axios for sending requests
Follow the Axios official page:
https://github.com/axios/axios
Passing Arguments via Query Params
|
|
Axios Common REST tips
For getting some data from firebase:
|
|
For posting some data to firebase:
|
|
For updating some data to firebase:
|
|
Deploy the REACT App to firebase Host
Firstly, you need to type the following command in the terminal:
|
|
You will generate a firebase.json file, and sometimes, it is empty. So you need to add the following code into that file:
|
|
remember key “public” stands for the folder you will be deploying. Generally, when we run
|
|
We will have a “build” folder. So we set “build” to the “public” key.
Deploy to AWS S3
Firstly, you need to install aws-cli. There are multiple ways to do that, but the easiest and efficient way I find is:
|
|
Then configure your aswcli by typing:
|
|
You can find your access key and secret access in MySecurity credentials, then expand Access keys (access key ID and secret access key) you will get that.
Then configure out aws s3:
Go to page: https://s3.console.aws.amazon.com/s3/home?region=us-east-1
Then click Create Bucket, enter Bucket name -> Set properties(as default) -> Set permission(Set manage public permissions as Grant public read access to this bucket)->Review(Click create bucket).
Configure that bucket
Click that bucket name,
then click Bucket Policy, copy the following testing to the Bukcet policy editor:
|
|
An easier way to do it is to use http://awspolicygen.s3.amazonaws.com/policygen.html (AWS Policy Generator)
Then Save it.
Go to Properties section: Click (Enable)Static website hosting -> The EndPoint is your website URL. And the index document is your defualt index page.
Uploading files
Type:
|
|
And remeber change the s3://testreact.com
to s3://yourBucketName
Now you can access the Endpoint to view your React App!