我需要在数据库中获取所有加油站的品牌名称,并在React组件中显示这些名称,但是我不确定应该使用哪个查询以及应该如何存储此数据。
我已经尝试从Firestore获取所需的信息并将其存储到redux中,但是它不起作用。我应该使用单独的异径管吗?
export const setStations = stations => ({
type: actionTypes.SET_STATIONS,
stations
});
export const setStationsNames = stationsNames => ({
type: actionTypes.SET_STATIONS_NAMES,
stationsNames
});
export const startSetStations = () => {
return dispatch => {
return db
.collection('stations')
.get()
.then(snapshot => {
const stations = [];
const stationsNames = [];
snapshot.docs.forEach(doc => {
stations.push(doc.data());
stationsNames.push(doc.data().brand);
});
dispatch(setStationsNames(stationsNames));
dispatch(setStations(stations));
});
};
};
{props.stations.map((station, i) => {
return (
<li key={i} className="station">
<h3 className="station__text">{station.brand}</h3>
</li>
);
})}
减速器
export default (state = stationsReducerDefaultState, action) => {
switch (action.type) {
case actionTypes.SET_STATIONS:
return action.stations;
default:
return state;
}
};