【发布时间】:2020-07-26 05:04:35
【问题描述】:
我试图将国家名称从 React 组件传递给 Redux 中的操作,但是当它到达操作创建器时,它变得未定义。我认为它与组件下方的 mapDispatchToProps() 部分有关,但我对 Redux 很陌生,所以我不完全确定。
它可以从 API 获取国家/地区列表,但是当我想将所选国家/地区传回 Actions.js 时,国家/地区变得未定义。
组件.js
import React, { useEffect, useState } from "react";
import { connect } from "react-redux";
import {
getCountryCasesAction,
getAllAvailableCountriesAction,
} from "../redux/Actions";
import { Form } from "semantic-ui-react";
function CasesByCountry({
countryOptions,
getAllAvailableCountries,
getCountryCases,
}) {
useEffect(() => {
getAllAvailableCountries();
}, [getAllAvailableCountries]);
const onDropdownOptionSelect = (e, result, country, countryData) => {
console.log(result.value);
getCountryCases(result.value);
};
return (
<div>
<Form.Dropdown
placeholder="Select Country"
fluid
selection
search
onChange={onDropdownOptionSelect}
options={
countryOptions &&
countryOptions.map((c) => {
return {
key: c.ISO2,
text: c.Country,
value: c.Country,
};
})
}
/>
</div>
);
}
const mapStateToProps = (state) => {
return {
countryOptions: state.data,
countryData: state.data,
};
};
const mapDispatchToProps = {
getAllAvailableCountries: getAllAvailableCountriesAction,
getCountryCases: getCountryCasesAction,
};
export default connect(mapStateToProps, mapDispatchToProps)(CasesByCountry);
Actions.js
import axios from "axios";
export const GET_WORLDWIDE_SUMMARY = "GET_WORLDWIDE_SUMMARY";
export const GET_COUNTRY_CASES = "GET_COUNTRY_CASES";
export const GET_ALL_AVAILABLE_COUNTRIES = "GET_ALL_COUNTRIES";
export const getWorldwideSummaryAction = () => async (dispatch, getState) => {
// const response = await axios
// .get
// // "https://cors-anywhere.herokuapp.com/" +
// // "process.env.REACT_APP_GET_WORLDWIDE_SUMMARY"
// ();
// console.log(response);
// return dispatch({
// type: GET_WORLDWIDE_SUMMARY,
// payload: response.data,
// });
};
export const getAllAvailableCountriesAction = () => async (
dispatch,
getState
) => {
const response = await axios.get("https://api.covid19api.com/countries");
console.log(response);
return dispatch({
type: GET_ALL_AVAILABLE_COUNTRIES,
payload: response.data,
});
};
export const getCountryCasesAction = () => async (
dispatch,
getState,
country
) => {
console.log("getCountryCasesAction");
const response = await axios.get(
`https://api.covid19api.com/dayone/country/${country}/status/confirmed`
);
console.log(response);
return dispatch({
type: GET_COUNTRY_CASES,
payload: response.data,
});
};
Reducer.js
import {
GET_WORLDWIDE_SUMMARY,
GET_ALL_AVAILABLE_COUNTRIES,
GET_COUNTRY_CASES,
} from "./Actions";
const initialState = {
data: "",
};
export const rootReducer = (state = initialState, action) => {
switch (action.type) {
case GET_WORLDWIDE_SUMMARY:
console.log(action.payload);
return {
...state,
data: action.payload,
};
case GET_ALL_AVAILABLE_COUNTRIES:
console.log(action.payload);
return {
...state,
data: action.payload,
};
case GET_COUNTRY_CASES:
console.log(action.payload);
return {
...state,
data: action.payload,
};
default:
return state;
}
};
【问题讨论】:
-
console.log(response);在getCountryCasesAction()里面的结果是什么? -
它记录了我从下拉菜单中选择的国家,这就是我想要传递给 Actions.js 的内容
标签: javascript reactjs redux thunk