In this exercise we will practice putting our Redux knowledge into action using the Star Wars API. We want to store all our state in the Redux store which we will manipulate using actions, update using reducers and then receive the changes where required.
- Create a search input component which we will use to search for characters by name in the Star Wars API. Each time the text in the component is updated by the user we want to emit an action which will update the
query
string in the store. Once thequery
string is updated we want to display the updated query string in our search component.
A good way to go about this is to create your initial Redux loop in order of execution.
- To enable you to initialise your store we have provided you with a
placeholder
reducer which does not do anything, but enables you to create arootReducer
with it. - Start by creating a Container component which will talk to Redux on behalf of the Search component
- Create the
Search
component which will be wrapped by the container. Create a receiver function inmapDispatchToProps
ofSearchContainer
which will be passed down toSearch
to emit thequery
to Redux. - Create an action which will emit the
query
fromSearch
viaSearchContainer
to the reducers. - Create a reducer which will add the
query
to Redux state and add the reducer torootReducer
. At this stage, we can also delete theplaceholder
from ourrootReducer
since we no longer need it now that we have an actual reducer. - Add a
mapStateToProps
method toSearchContainer
so we can readquery
state from Redux store and pass it down toSearch
to display.
- Add a submit event handler to the search component. When a user submits a search we want to use the
query
string to search the Star Wars API. Use thehttps://swapi.co/api/people/?search=<query string>
api end point to get the search results, add them to the store using a reducer and display them to the user. To display results create a new Results component with a Results container to retrieve data from Redux state.
Stretch goal
- Add a select element to your search component which will allow us to select which end point we want to search
people
,vehicles
,planets
,films
,species
orstarships
. Use the value the from the search input to set api end point for the search.