- Use the
useEffect
hook to fetch data from an API - Use event handlers with
fetch
to send data to an API - Update state after receiving data from an API
We're going to continue working on our quiz app, this time from the administrative side of things! We want our quizmasters to be able to view, add, edit, and delete the existing quizzes so they can change the content for the rest of our users.
There's some starter code set up, so you won't need to create any new components
for this lab. Your goal will be to work with fetch
and interact with our quiz
API to perform the necessary CRUD operations to complete these deliverables.
As usual, make sure to run npm install
to install the necessary dependencies.
For this lab, we'll be using json-server
to create a RESTful API with our quiz
data. You can run json-server
by running npm run server
(the command for
this is in the package.json
file). Once your server is running, go to
http://localhost:4000/questions
in your browser and have a look at the
question data.
Then, in a new terminal tab, run npm start
to run the React application.
When the application loads, get all the questions from
http://localhost:4000/questions
and display them using the QuestionList
component.
You'll need to add useState
and useEffect
for this deliverable, but it's up
to you to decide where it belongs! Think about which components will need access
to the question data.
When the user clicks the 'New Question' button, a form will be displayed for creating a new question. This form is already set up as a controlled form, so your responsibility will be to send this form data to our API when the form is submitted.
For the API to work, you'll need to format your POST request like this:
POST /questions
Required Headers:
{ "Content-Type": "application/json" }
Body:
{
"prompt": string,
"answers": array of strings,
"correctIndex": integer
}
In addition to updating the form, you should display the new question in the
QuestionList
component by updating state.
NOTE: because json-server
doesn't have any validations, if you make any
mistakes and send the body of your request in the wrong format, you'll need to
manually delete the entry from the db.json
file.
When the user clicks the 'View Questions' button, a list of all the questions should show up (from deliverable 1). When the delete button is clicked, the question should be removed from the list by updating state. It should also be deleted on the server.
Make sure to include the id of the question you're trying to delete in your request's url!
When the user clicks the 'View Questions' button, a list of all the questions should show up (from deliverable 1). When the dropdown for the correct answer is changed, the question should be updated on the server. It should also be updated in state.
For the API to work, you'll need to format your PATCH request like this:
PATCH /questions/:id
Required Headers:
{ "Content-Type": "application/json" }
Body:
{
"correctIndex": integer
}
Make sure to include the id of the question you're trying to update in your request's url!
react-hooks-fetch-crud-lab's People
Forkers
philipmo59 sandyjtech msbluemu seanquinn917 ellenbrianne eblaekerdiker johncorn patrob787 kylanalicia michellemwangi01 samsom-muchiri zyzz420 jamesirungume ntimamaa barondevke bii-teki karanja03 wanjira-faith samuelmaiko whiplade kiarie-kariuki moseschengo29 rotichkipkoech peggykimotho donell895 29kaggia christine-m9 briankipr kipmaiyo nenoeglah timothyndarau winny52 morinke01 cnnjuguna bisinle prodigysec ajowi254 elsawanjiru brighton-flemming adrianmunandi joy19anyango isma1huz brendabii voste126 blackcrystal0000 barakatimothy annndiga macray-korir kiplangatwayne ian32-del noahntabo23 jmakateto judy-chelangat yegon-gk kibetian cherise43 gaitano123 john-maluki uel-s alex-gikungu hawamariah mramario carolkoech vitamaxoduol mugeshi doubleg254 charity-wachira abogeerick clyfe-008 ahmednoorzakaria ibrahimmohamed3 anita-mutemi stan-g7 nataliewanjiru dkabucho arnold-aswan hellenwamaitha wanyama23 meshackpatrick mitunda mercy-chepchirchir mary446 nganga-a noelle-wavinya-maingi njagiiii miriamrehema ezramachoka papsel gilbert-ku houstin0 naomicherono mwaniki-titus shadrack5 waitheravivyanne slonetech victorodambatafwa22 davidndiba vanyikuze fay-simotwo joyolagoreact-hooks-fetch-crud-lab's Issues
Test passes incorrectly
Canvas Link
https://learning.flatironschool.com/courses/6873/assignments/246254?module_item_id=580806
Concern
The test for the " ✓ updates the answer when the dropdown is changed (30 ms)" passes before I do anything, and someone else told me the same happened to them. Even if I don't write the code for it, it passes.
Additional Context
No response
Suggested Changes
No response
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.