Skip to content

Instantly share code, notes, and snippets.

@petekgithub
petekgithub / index.jsx
Last active June 9, 2022 06:46
async functions in useEffect
import useEffect from 'react'
useEffect(() => {
// declare the data fetching function
const fetchmyTarot = async() => {
// get data from the api
const response = await fetch("https://rws-cards-api.herokuapp.com/api/v1/cards/random?n=10")
// handle response error
@petekgithub
petekgithub / app.tsx
Last active May 31, 2022 11:43
calling-api-and-calling-data
function App() {
const [play,setPlay] = useState([])
const [error,setError] = useState({})
useEffect(() => {
.fetch('https://jsonplaceholder.typicode.com/todos')
.then(response => response.json())
.catch(err => setError(err))
},[])
}
@petekgithub
petekgithub / index.jsx
Created June 13, 2022 13:39
Class-Component(Stateful)
import React, { Component } from 'react';
class StateExample extends Component {
constructor(){
super();
this.state = {
car_brand: 'Volvo',
car_color: 'bluesky'
}
}
@petekgithub
petekgithub / index.js
Created June 13, 2022 13:42
Functional-Component(Stateless)
import React from 'react';
function Example(props) {
return (
<div>
<h1>Class Comonent</h1>
<p>{this.state.car_brand}</p>
<p>{this.state.car_colord}</p>
</div>
)
@petekgithub
petekgithub / api-requests-with-useEffect
Last active August 15, 2022 07:35
api-requests-with-useEffect
import React, { useState, useEffect } from "react";
const TodoList = () => {
const [todos, setTodos] = useState();
useEffect(() => {
axios.get(`https://jsonplaceholder.typicode.com/posts`)
.then((response) => {
const responseTodos = response.data;
@petekgithub
petekgithub / nextjs-staticgeneration
Last active August 28, 2022 11:51
next.js-fetch-staticgenerationway
export default function Home({posts}) {
<div>
{posts.map(post => (
<div key={post.id}>
<h1>{post.title}</h1>
<p>{post.id}</p>
</div>
))}
</div>