Reputation:
I'm attempting to reconfigure a PixaBay clone application to Redux. The application sets the searchText state to the value of the input, then triggers an Axios GET request callback as the user types a search text, then images are retrieved.
I've received a parsing error on my reducer, but I don't understand the error as I believe the code is correct. Could someone kindly help me solve the issue? Thank you!
CONTAINER
import React, { Component } from 'react';
import { fetchPhotos } from '../actions/actions';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import TextField from 'material-ui/TextField';
import ImageResults from '../imageResults/ImageResults';
class Search extends Component {
FetchPhotosHandler = (e) => {
this.props.fetchPhotos(e);
}
render() {
console.log(this.props.images);
return (
<div>
<TextField
name="searchText"
value={this.props.searchText}
onChange={this.FetchPhotosHandler}
floatingLabelText="Search for photos"
fullWidth={true} />
<br />
{this.props.images.length > 0 ? (<ImageResults images={this.props.images} />) : null}
</div>
);
}
}
function mapDispatchToProps(dispatch) {
return bindActionCreators({ fetchPhotos, dispatch});
}
export default connect(null, mapDispatchToProps)(Search);
ACTION
import axios from 'axios';
export const FETCH_PHOTOS = 'FETCH_PHOTOS';
const ROOT_URL = 'https://pixabay.com/api';
const API_KEY = 'my_api_key';
const searchText = '';
export function fetchPhotos(e) {
const url = `${ROOT_URL}/?key=${API_KEY}&q=${searchText}&image_type=photo`;
const request = this.setState({searchText: e.target.value}, () => {
axios.get(url)
.then(response => {
this.setState({images: response.data.hits});
})
.catch(error => {
console.log(error)
});
});
return {
type: FETCH_PHOTOS,
payload: request
};
}
REDUCER
import { FETCH_PHOTOS } from '../actions/actions';
const initialState = {
searchText: '',
images: []
}
const reducer = (state = initialState, action) => {
switch(action.type) {
case FETCH_PHOTOS:
return {
...state,
action.data.hits
};
default:
return state;
}
}
export default reducer;
ERROR
./src/reducers/reducer.js
Line 16: Parsing error: Unexpected token, expected ","
14 | return {
15 | ...state,
> 16 | action.data.hits
| ^
17 | };
18 | default:
19 | return state;
Upvotes: 0
Views: 396
Reputation: 2332
You're returning an object, so you will need to assign a key for your API response.
return {
...state,
images: action.data.hits,
};
Upvotes: 3