I created a very simple app with only React and now I want to change it so, I can use Redux in it. (I know Redux is not needed here, but I'm just trying to learn Redux).
SearchBar.js - Only React
import React, { Component } from "react";
class SearchBar extends Component {
state = {
inputValue: ""
};
handleFormSubmit = e => {
e.preventDefault();
this.props.onSubmit(this.state.inputValue);
};
render() {
return (
<div className="header">
<h1>Search for images on Unsplash</h1>
<form onSubmit={this.handleFormSubmit} className="ui form">
<input
type="text"
placeholder="Type here to search for images"
value={this.state.inputValue}
onChange={e => this.setState({ inputValue: e.target.value })}
/>
</form>
</div>
);
}
}
export default SearchBar;
App.js - Only React
import React, { Component } from "react";
import axios from "axios";
import SearchBar from "./components/SearchBar";
import ImageList from "./components/ImageList";
class App extends Component {
state = {
images: []
};
onSearchSubmit = async inputValue => {
const API_KEY =
"<MY API KEY FOR UNSPLASH>";
const response = await axios.get(
`https://api.unsplash.com/search/photos?page=1&query=${inputValue}&client_id=${API_KEY}`
);
this.setState({ images: response.data.results });
};
render() {
return (
<>
<SearchBar onSubmit={this.onSearchSubmit} />
<div>
<ImageList images={this.state.images} />
</div>
</>
);
}
}
export default App;
Using Redux
I put the redux-version on codeSandBox. Of course it's not working yet.
Here are my changes so far:
App.js with redux
import React, { Component } from "react";
import { Provider } from "react-redux";
import store from "./store";
import SearchBar from "./components/SearchBar";
import ImageList from "./components/ImageList";
import "./app.scss";
class App extends Component {
render() {
return (
<Provider store={store}>
<SearchBar onSubmit={this.onSearchSubmit} />
<div>
<ImageList images={this.state.images} />
</div>
</Provider>
);
}
}
export default App;
store.js
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers";
const initialState = {};
const middleware = [thunk];
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
);
export default store;
fetchAction.js
import axios from "axios";
export const FETCH_DATA = "fetch_data";
// Getting all images
export const getImages = inputValue => async dispatch => {
const API_KEY =
"<MY API KEY FOR UNSPLASH>";
const res = await axios.get(
`https://api.unsplash.com/search/photos?page=1&query=${inputValue}&client_id=${API_KEY}`
);
console.log(res.data.results);
dispatch({
type: FETCH_DATA,
payload: res.data.results
});
};
index.js inside reducers folder
import { combineReducers } from "redux";
import fetchReducer from "./fetchReducer";
export default combineReducers({
images: fetchReducer
});
fetchReducer.js
import { FETCH_DATA } from "../actions/fetchAction";
const initialState = {};
export default function(state = initialState, action) {
switch (action.type) {
case FETCH_DATA:
return {
...state
};
default:
return state;
}
}
But, I have two questions:
- Where should I use connect? in App.js or in SearchBar.js?
If I add the following to my component, where I use connect:
const mapStateToProps = state => ({ images: });
export default connect( mapStateToProps, { getImages } )(SearchBar);
What would be the value of images inside mapStateToProps?
I put the redux-version on codeSandBox. Of course it's not working yet.