File tree Expand file tree Collapse file tree 1 file changed +14
-5
lines changed 
docs/react-testing-library Expand file tree Collapse file tree 1 file changed +14
-5
lines changed Original file line number Diff line number Diff line change @@ -174,6 +174,11 @@ fetch.js
174174import  React , { useState , useReducer  } from  ' react' 
175175import  axios  from  ' axios' 
176176
177+ const  initialState  =  {
178+  error:  null ,
179+  greeting:  null ,
180+ }
181+ 
177182function  greetingReducer (state , action ) {
178183 switch  (action .type ) {
179184 case  ' SUCCESS' 
@@ -185,7 +190,7 @@ function greetingReducer(state, action) {
185190 case  ' ERROR' 
186191 return  {
187192 error:  action .error ,
188-  greeting:  null 
193+  greeting:  null , 
189194 }
190195 }
191196 default : {
@@ -195,18 +200,22 @@ function greetingReducer(state, action) {
195200}
196201
197202export  default  function  Fetch ({ url }) {
198-  const  [{ error , greeting  }, dispatch ] =  useReducer (greetingReducer)
203+  const  [{ error , greeting  }, dispatch ] =  useReducer (
204+  greetingReducer,
205+  initialState
206+  )
199207 const  [buttonClicked , setButtonClicked ] =  useState (false )
200208
201209 const  fetchGreeting  =  async  () =>  {
202-  axios .get (url)
203-  .then ((response ) =>  {
210+  axios
211+  .get (url)
212+  .then (response  =>  {
204213 const  { data  } =  response
205214 const  { greeting  } =  data
206215 dispatch ({ type:  ' SUCCESS' 
207216 setButtonClicked (true )
208217 })
209-  .catch (( error )  =>  {
218+  .catch (error  =>  {
210219 dispatch ({ type:  ' ERROR' 
211220 })
212221 }
                         You can’t perform that action at this time. 
           
                  
0 commit comments