Working example in here.
const App = () => {
const [myApi, setMyApi] = useState([]);
const [data, setData] = useState([]); // add your data to here
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage] = useState(10);
const [searchUser, setSearchUser] = useState("");
useEffect(() => {
fetch("https://randomuser.me/api/?results=50")
.then(data => data.json())
.then(json_result => {
setData(json_result.results); // set your data to state
let myApi = renderData(json_result.results); // render your component
setMyApi(myApi); // set it to state
});
}, []);
const renderData = (data) => {
return data.map((item, idx) => {
return (
<div key={idx}>
<img src={item.picture.thumbnail} alt="" /> {item.name.first}
<hr />
</div>
);
});
}
// get current post
const indexOfLastPost = currentPage * postsPerPage; // 1 * 10 = 10
const indexOfFirstPost = indexOfLastPost - postsPerPage; // 10 - 10 = 0
const currentPosts = myApi?.slice(indexOfFirstPost, indexOfLastPost); // 0 to 10
// search users by user input
const handleSearchInput = event => {
setSearchUser(event.target.value);
const newData = renderData(data.filter(item => item.name.first.toLowerCase().includes(event.target.value))); // render filtered data
setMyApi(newData); // and set it to state
};
const paginate = pageNumber => setCurrentPage(pageNumber);
return (
<div>
<Search onChange={handleSearchInput} />
<Pagination
postsPerPage={postsPerPage}
totalPosts={myApi?.length}
paginate={paginate}
/>
{currentPosts}
</div>
);
};
const Search = ({ onChange }) => {
return (
<div>
<input
type="text"
autoFocus={true}
placeholder="search users"
onChange={onChange}
/>
</div>
);
};
Answer from Rajitha Udayanga on Stack OverflowGitHub
github.com › suttonj06 › random-user-api-react
GitHub - suttonj06/random-user-api-react: Utilize Random User API with ReactJS
Utilize Random User API with ReactJS. Contribute to suttonj06/random-user-api-react development by creating an account on GitHub.
Author suttonj06
YouTube
youtube.com › coding shiksha
React Fetch API Random User API Example | HTTP Requests in React - YouTube
Buy the full source code of application here:https://buy.stripe.com/4gwcQ66FbeFa8jC4xbSubscribe for more Videos: https://www.youtube.com/channel/UCR6d0EiC3G4...
Published February 19, 2020 Views 10K
Videos
50:17
🔴ReactJS Random User Generator App || ReactJS Tutorial - YouTube
01:05:13
ReactJs and TailwindCSS Random User Generator App - YouTube
13:34
Build a Javascript Random User Generator With Pagination Using ...
08:13
Fetch Data from an API in React.js - Part 12 - YouTube
CodeSandbox
codesandbox.io › s › randomuser-api-in-react-rkoqp
Randomuser API in React - CodeSandbox
June 24, 2019 - Randomuser API in React by katarzyna-stepczynska using react, react-dom, react-scripts
Top answer 1 of 2
2
Working example in here.
const App = () => {
const [myApi, setMyApi] = useState([]);
const [data, setData] = useState([]); // add your data to here
const [currentPage, setCurrentPage] = useState(1);
const [postsPerPage] = useState(10);
const [searchUser, setSearchUser] = useState("");
useEffect(() => {
fetch("https://randomuser.me/api/?results=50")
.then(data => data.json())
.then(json_result => {
setData(json_result.results); // set your data to state
let myApi = renderData(json_result.results); // render your component
setMyApi(myApi); // set it to state
});
}, []);
const renderData = (data) => {
return data.map((item, idx) => {
return (
<div key={idx}>
<img src={item.picture.thumbnail} alt="" /> {item.name.first}
<hr />
</div>
);
});
}
// get current post
const indexOfLastPost = currentPage * postsPerPage; // 1 * 10 = 10
const indexOfFirstPost = indexOfLastPost - postsPerPage; // 10 - 10 = 0
const currentPosts = myApi?.slice(indexOfFirstPost, indexOfLastPost); // 0 to 10
// search users by user input
const handleSearchInput = event => {
setSearchUser(event.target.value);
const newData = renderData(data.filter(item => item.name.first.toLowerCase().includes(event.target.value))); // render filtered data
setMyApi(newData); // and set it to state
};
const paginate = pageNumber => setCurrentPage(pageNumber);
return (
<div>
<Search onChange={handleSearchInput} />
<Pagination
postsPerPage={postsPerPage}
totalPosts={myApi?.length}
paginate={paginate}
/>
{currentPosts}
</div>
);
};
const Search = ({ onChange }) => {
return (
<div>
<input
type="text"
autoFocus={true}
placeholder="search users"
onChange={onChange}
/>
</div>
);
};
2 of 2
1
Since you're useEffect has [] (empty array) as the dependency, you're user fetching logic will only be called once i.e. on the initial rendering. You can add searchUser as useEffect's dependency so you can fetch users whenever the searchUser text changes.
Stack Overflow
stackoverflow.com › questions › 65444356 › how-to-get-user-details-of-user-from-randomuser-api
How to get user details of user from randomuser API?
import React, {useState, useEffect} from 'react' import { useParams } from 'react-router-dom'; export const UserDetail = () => { const {userid} = useParams() const [result, setResult] = useState([]); useEffect(() => { fetch(`/user-detail/${userid}`).then(res=>res.json()).then(result=> { console.log(result) setResult(result); }) }, []) return ( <div> </div> ) } ... Here is another free API you can use instead if you are interested in generating random users with details.
Random User Generator
randomuser.me
Random User Generator | Home
A free, open-source API for generating random user data. Like Lorem Ipsum, but for people.
YouTube
youtube.com › watch
Javascript Fetch API Example – Build a Random User Generator App - YouTube
Buy the full source code of application here:https://buy.stripe.com/dR6dUa1kR9kQdDWbZ0
Published January 19, 2019
CodePen
codepen.io › KristofferTroncoso › pen › JvyWbz
Profile Cards (React, Random-User-API)
ct.Component { static defaultProps = { name: 'Bob', email: 'default@email.com', pic: 'default.jpg' } getNewUser = e => { fetch('https://randomuser.me/api/') .then(response => response.json()) .then(data => { let firstName = data.results[0].name.first[0].toUpperCase() + data.results[0].name.first.slice(1); let lastName = data.results[0].name.last[0].toUpperCase() + data.results[0].name.last.slice(1); let name = firstName + ' ' + lastName; let email = data.results[0].email; let pic = data.results[0].picture.large; let username = data.results[0].login.username; this.props.gettingUser({name, email
GitHub
github.com › topics › randomuser-api
randomuser-api · GitHub Topics · GitHub
This website is created using React JS. The data used is fetch from https://randomuser.me/api/ .
RapidAPI
rapidapi.com › guides › build-random-user-generator-app
How to build a Random User Generator App using ...
Guides by Rapid offer short and long form API Development content with interactive examples and visuals to help you become a pro API Developer.
Top answer 1 of 3
1
As mentioned on the GITHUB docs, you can implement the fetch like
fetch('https://randomuser.me/api/')
.then((response) => {
return response.json()
}).then((d) => {
console.log('parsed json', d)
this.setState({
data: d
});
}).catch(function(ex) {
console.log('parsing failed', ex)
this.setState({
requestFailed: true
})
})
CODEPEN
2 of 3
1
fetch method should be
fetch('your_url')
.then (
response => {
if (response.status !== 200) {
return 'Error. Status Code: ' + response.status
}
response.json().then(result => console.log(result)) // do sth with data
}
)
.catch(function(err) {
console.log('Opps Error', err)
})
GitHub
github.com › damarendra › react-native-random-user-app
GitHub - damarendra/react-native-random-user-app: Fetch and Display API from randomuser.me/api
Fetch and Display Random User from https://randomuser.me/api/ { "axios": "^0.18.0", "prop-types": "^15.7.2", "react": "16.6.3", "react-native": "0.58.5", "react-native-elements": "^1.1.0", "react-native-gesture-handler": "^1.0.16", "react-native-vector-icons": "^6.3.0", "react-navigation": "^3.3.2", "react-redux": "^6.0.1", "redux": "^4.0.1", "redux-actions": "^2.6.4", "redux-saga": "^1.0.2", "reselect": "^4.0.0" }
Author damarendra