๐ฅ You can watch a full video tutorial on how to create your React application with Context.API and Flux following this template.
Create your first view, add it to your application routes, create your first component, add styles and use the context API.
Make sure to follow the steps on running the project first, then come back to this list, if you are using Gitpod or Codespaces it is possible that the project is already running.
It's time to add some code: Everything starts at the application layout (js/layout.js
), this file is like a table of contents where all of your application pages will be added before they can be rendered by React and the browser, you can read more about React Router here.
We already added a few routes like Home
, Demo
and Single
, these views are useful examples of the most frequent things you usually need in a project.
All of your application pages will be added into the js/pages
folder, each of them will be a separate React component.
Let's open the <Demo>
view at js/pages/demo.js
.
As you can see, the demo.js
page is just a React component, here are additional things to notice:
AppContext
that will be used for dealing with any global information needed from other views or the application; And the demo.css
that will be used to add any CSS classes and styles used on this view in particular.1const { store, actions } = useContext(Context);
All the application styles are saved inside the styles
folder, we usually have a separate style for each component.
You can update the styles/index.css
or create new .css
files inside styles/
and import them into your current CSS or JS files depending on your needs.
For example, if you would want to create a class background-blue
that makes the home background blue you need to do something like this:
.background-blue
into the styles/home.css
.<div className="background-blue">
into your HTML page at js/pages/home.js
.Usually you want most of your HTML application to be split into components that can be re-used.
All the components are meant to be created into the js/components
and then you can import them into the pages that will use them.
๐ We are using functional components (instead of class oriented components) since it's the best practice in the industry.
For example, if we want to add a new <Card>
component that replicates the classic bootstrap card, we can create a js/components/Card.js
with the following code:
1export const Card = () => ( 2 const [state, setState] = useState('code here'); //using the state (if needed) 3 return <div className="card"> 4 <div className="card-body"> 5 <h5 className="card-title">Card title</h5> 6 <h6 className="card-subtitle mb-2 text-muted">Card subtitle</h6> 7 <p className="card-text">Some quick example text to build on the card title and make up the bulk of the cards content.</p> 8 </div> 9 </div> 10);
Now that we have the Card
component we can incorporate it to our Home.js
page with the following steps:
<Card>
tag inside the HTML that your page returns, for example:1import { Card } from "../component/Card.js"; 2 3export const Home = () => { 4 const { store, actions } = useContext(Context); 5 6 return ( 7 <div className="text-center mt-5"> 8 <h1>Hello Rigo!!</h1> 9 <Card /> 10 </div> 11 ); 12};
Important notes
{
when importing the component like this: { Card }
.<Card />
tag being used in line 9 of the Home.js
file.This boilerplate comes with a centralized general Context API. The file js/store/flux.js
has a base structure for the store, we encourage you to change it and adapt it to your needs.
The Provider
is already set. You can consume from any component using the useContext
hook to get the store
and actions
from the Context. Check /views/demo.js
to see a demo.
1import { Context } from "../store/appContext"; 2 3const MySuperPage = () => { 4 //here you use useContext to get store and actions 5 const { store, actions } = useContext(Context); 6 7return <div>{/* you can use your actions or store inside the html */}</div> 8}
Login (you need to have an account):
1npm i vercel -g && vercel login
Deploy:
1vercel --prod
โ Note: If you don't have an account just go to vercel.com, create an account and come back here.
1npm run deploy
๐ Note: You will need to configure Github pages for the branch gh-pages
Note: Make sure you are using node version 14+
1npm install
1cp .env.example .env
1npm run start