Я получу больше обновлений о вашем проекте.см. примеры кода и коробки!
import React from "react";
import SearchBar from "./SearchBar";
import ImageList from "./ImageList";
import { fetchImages } from "../actions";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
class App extends React.Component {
onSearchSubmit = term => {
this.props.fetchImages(term);
};
render() {
return (
<div className="ui container" style={{ marginTop: "10px" }}>
<SearchBar onSubmit={this.onSearchSubmit} />
<ImageList images={this.props.images} />
</div>
);
}
}
export default connect(
state => ({
images: state.images
}),
dispatch => ({
fetchImages: bindActionCreators(fetchImages, dispatch)
})
)(App);
Действие:
export function fetchImages(term) {
return function action(dispatch, getState) {
unsplash
.get("/search/photos", {
params: { query: term }
//use the path on unsplash for image searching
})
.then(res => {
console.log(res.data.results);
dispatch({ type: types.FETCH_BUTTON, payload: res.data.results });
});
};
}
И состояние:
export const images = (state = [], action) => {
switch (action.type) {
// ...
case types.FETCH_BUTTON:
return Object.assign([], state, [...action.payload]);
default:
return state;
}
};
Рабочий пример: