baseURL
과 키를 환경변수 설정해준다.REACT_APP_
키워드가 앞에 붙은 변수들만 환경 변수로 사용 가능하다.
env
파일에 발급 받은 키를 넣어준다..gitignore
에 .env
를 추가해준다.playlist
의 값들이 잘들어온다. url로 불러온 아이템의 필수 매개변수인 part의 snippet
을 불러오지 않아서 snippet을 찾을수 없었던것이다!
params
의 매개변수인 part의 snippet
을 불러왔다.<any>
를 넣어서 임시적으로 해결함 stackoverflow 참고
// 1. action items
export const MEDIA_VIDEOS_SUCCESS = 'MEDIA_VIDEOS_SUCCESS';
export const MEDIA_VIDEOS_FAIL = 'MEDIA_VIDEOS_FAIL';
export const MEDIA_VIDEOS_REQUEST = 'MEDIA_VIDEOS_REQUEST';
import {
MEDIA_VIDEOS_FAIL,
MEDIA_VIDEOS_REQUEST,
MEDIA_VIDEOS_SUCCESS,
} from '../ActionType';
import { request } from '../../api';
interface SystemError {
code: string;
message: string;
}
export const getSearchVideos = () => async (dispatch: any) => {
try {
dispatch({
type: MEDIA_VIDEOS_REQUEST,
});
const { data } = await request('/search', {
params: {
part: 'snippet',
channelId: 'UCMki_UkHb4qSc0qyEcOHHJw',
regionCode: 'KR',
maxResults: 50,
pageToken: '',
type: 'video',
},
});
dispatch({
type: MEDIA_VIDEOS_SUCCESS,
payload: {
// video: data.items,
search: data.items,
nextPageToken: data.nextPageToken,
},
});
} catch (error) {
const err = error as SystemError;
if (err.code === 'ENOENT') {
console.log('Someting wrong!');
}
dispatch({
type: MEDIA_VIDEOS_FAIL,
payload: error,
});
}
};
import {
MEDIA_VIDEOS_FAIL,
MEDIA_VIDEOS_SUCCESS,
MEDIA_VIDEOS_REQUEST,
} from '../ActionType';
export const MediaVideos = (
state = {
search: [],
loading: false,
nextPageToken: null,
},
action: any,
) => {
const { type, payload } = action;
switch (type) {
case MEDIA_VIDEOS_SUCCESS:
return {
...state,
search: payload.search,
loading: false,
nextPageToken: payload.nextPageToken,
};
case MEDIA_VIDEOS_FAIL:
return {
...state,
loading: false,
error: payload,
};
case MEDIA_VIDEOS_REQUEST:
return {
...state,
loading: true,
};
default:
return state;
}
};
const middlewares = [ReduxThunk];
const rootReducer = combineReducers({
MediaVideos,
});
const store = createStore(
rootReducer,
compose(applyMiddleware(...middlewares)),
);
export default store;
export type RootState = ReturnType<typeof rootReducer>;
const dispatch = useDispatch<any>();
useEffect(() => {
dispatch(getSearchVideos());
}, [dispatch]);
const { search } = useSelector((state: RootState) => state.MediaVideos);