visit
We are going to create a badge to award users who have received more than 5 votes. We would mock the JSON data locally and give every user that has more than five-votes
a badge using react and redux to consume the data.
In a blank Create React App project, create a local JSON file named data.json
inside the public directory.
Your Fetch API calls made from a React component always look for files or any other relevant assets inside this public directory. Create-React-App
doesn't put your assets automatically inside this directory during compilation so you have to do this manually.
[
{
"id": 1,
"name": "Daniel",
"email": "[email protected]",
"post": "I love football",
"votes": 5
}
]
npm i -s react-redux redux axios redux-thunk
or
yarn add react-redux redux axios
Also, we would install fontaweasome
, where we would import icons as a badge.
npm i --save @fortawesome/fontawesome-svg-core
npm install --save @fortawesome/free-solid-svg-icons
npm install --save @fortawesome/react-fontawesome
or
yarn add @fortawesome/fontawesome-svg-core
yarn add @fortawesome/free-solid-svg-icons
yarn add @fortawesome/react-fontawesome
In the src/ directory let's make 6 Files
const USER = {
LOAD: "REQUEST_USERS_DATA",
LOAD_SUCCESS: "RECEIVE_USERS_DATA",
};
export default USER;
import USER from "./constants";
const initalState = {
usersData: [],
isLoading: false,
isError: false,
};
const reducer = (state = initalState, action) => {
switch (action.type) {
case USER.LOAD:
return {
...state,
isLoading: true,
isError: false,
};
case USER.LOAD_SUCCESS:
return {
...state,
usersData: action.usersData,
isLoading: false,
};
default:
return state;
}
};
export default reducer;
import axios from "axios";
import USER from "./constants";
export const requestUsers = (data) => async (dispatch) => {
dispatch({
type: USER.LOAD,
});
try {
const json = await axios.get("data.json");
console.log(json);
dispatch({
type: USER.LOAD_SUCCESS,
usersData: json.data,
isError: false,
});
} catch (e) {
dispatch({
type: USER.LOAD_SUCCESS,
usersData: [],
isError: true,
});
}
};
In the index file we would import fontawesome
so any components could inherit from it.
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { store } from "./store";
import { Provider } from "react-redux";
import "./fontAwesome";
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.getElementById("root")
);
import { library } from "@fortawesome/fontawesome-svg-core";
import {
faGithubAlt,
faGoogle,
faFacebook,
faTwitter,
faJediOrder,
} from "@fortawesome/free-brands-svg-icons";
library.add(faGithubAlt, faGoogle, faFacebook, faTwitter, faJediOrder);
import thunkMiddleware from "redux-thunk";
import { createStore, applyMiddleware } from "redux";
import reducer from "./reducer";
export const store = createStore(reducer, applyMiddleware(thunkMiddleware));
[
{
"id": 1,
"name": "Daniel",
"email": "[email protected]",
"post": "I love football",
"votes": 5
}
]
We import the following hooks from react-redux: useSelector
and useDispatch
.
useSelector
is a function that takes the current state as an argument and returns whatever data you want from it and it allows you to store the return values inside a variable within the scope of your functional components instead of passing them down as props.
useDispatch
is equivalent to mapDispatchToProps
. We will invoke useDispatch
and store it to a variable, dispatch. This hook returns a reference to the dispatch function from the Redux store.
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";
import { requestUsers } from "./action";
import data from "./data.json";
import "./App.css";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
const App = () => {
const { usersData, isLoading } = useSelector((state) => state);
const dispatch = useDispatch();
useEffect(() => {
dispatch(requestUsers(data));
}, []);
return (
<>
{isLoading && <div className="loading">Data loading...</div>}
{usersData.map((user) => {
return (
<div key={user.id} className="container">
<div className="content">
<h1>{user.name}</h1>
<span>{user.email}</span>
<h3>{user.post}</h3>
Votes: {user.votes}
{user.votes >= 5 ? (
<div>
<FontAwesomeIcon icon={["fab", "jedi-order"]} size="3x" />
</div>
) : (
<p>Get up to five votes to have a badge</p>
)}
</div>
</div>
);
})}
</>
);
};
export default App;
Redux is a predictable state container for javascript.
Happy coding!