Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Improvement suggestion around dispatching a fetch action from within a functional component and handling errors #186

Open
@dcs3spp

Description

@dcs3spp

Hi,

Firstly, I have found the patterns in the guide very helpful for configuring actions, epics, reducers etc. However, I do not understand how to dispatch a fetch API request action and subsequently bind to a fetch successful action. I have managed to create an epic for a fetch API request that then triggers a success or error action depending upon the success/failure of the API request.

I do not understand how to hook this up to a functional component, as highlighted in the comments of the CourseList functional component code listing below:

CourseList Functional Component

importReact,{useEffect}from'react';importGridfrom'@material-ui/core/Grid';import{GridSpacing}from'@material-ui/core/Grid';importCoursefrom'../../components/Course/Course';import{CourseasCourseModel}from'../../redux/features/course/model';typeProps={courses:CourseModel[];onFetchCourseRequest: ???// I want to bind this to the fetch courses action but do not understand how?? what type should this be??};exportconstCourseList:React.FC<Props>=props=>{const{ courses, onFetchCourseRequest}=props;// functional equivalent of componentDidMount// this only gets triggered once, since// dependency list is empty// I want this to to trigger the dispatch of a FETCH_COURSE_LIST action and bind to resulting FETCH_COURSE_LIST// I do not understand how to do this from examples....useEffect(()=>{onFetchCourseRequest();},[]);return(<divstyle={{marginTop:20,padding:30}}>{<Gridcontainerspacing={2asGridSpacing}justify="center">{courses.map(element=>(<Griditemkey={element.courseID}><Coursecourse={element}/></Grid>))}</Grid>}</div>);};

CourseListConnected

import{RootState}from'ReduxTypes';import{connect}from'react-redux';import{CourseList}from'./CourseList';import{courseActions,courseSelectors}from'../../redux/features/course';constmapDispatchToProps={onFetchCoursesRequest:courseActions.fetchCoursesRequest,};constmapStateToProps=(state:RootState)=>({courses:courseSelectors.getReduxCourses(state.courses.fetchCoursesSuccess),});constCourseListConnected=connect(mapStateToProps,mapDispatchToProps,)(CourseList);exportdefaultCourseListConnected;

Epic for Fetching a Course from API

import{RootAction,RootState,Services}from'ReduxTypes';import{Epic}from'redux-observable';import{isOfType}from'typesafe-actions';import{of}from'rxjs';import{catchError,filter,ignoreElements,map,switchMap,}from'rxjs/operators';import{fetchCoursesFail,fetchCoursesSuccess}from'./actions';importconstantsfrom'./constants';exportconstfetchCoursesRequestAction:Epic<RootAction,RootAction,RootState,Services>=(action$,state$,{ courseServices})=>action$.pipe(filter(isOfType(constants.FETCH_COURSES_REQUEST)),switchMap(action=>courseServices.default.getCourses().pipe(map(courses=>fetchCoursesSuccess(courses)),catchError(error=>of(fetchCoursesFail(error))),),),ignoreElements(),// ignore everything except complete and error, template does this);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp