状态不更新并出现错误:TypeError:调度不是函数
我目前正在从事一个个人项目。我使用 API 来获取新闻文章:我在应用程序渲染时获取最新新闻,而不是单个问题,商店获取所需的所有数据等等。 但是,当我开始想要搜索主题时,没有任何反应,并且在浏览器控制台中出现此错误:“TypeError:调度不是函数”
这是发生错误的操作文件
import { API_URL_BASE, API_KEY, FETCH_API } from "./constants";
import { ApiReducerInterface } from "./apiReducerInterface";
import axios from "axios";
export const fetchMainTopics = () => async (dispatch: any) => {
try{
const res = await axios.get(`${API_URL_BASE}top-headlines?country=fr&apiKey=${API_KEY}`);
await dispatch(fetchAPI(res.data.articles));
}
catch(error) {
console.log(error);
}
}
export const searchTopics = (search: string) => async (dispatch: any) => {
try{
const res = await axios.get(`${API_URL_BASE}everything?q=${search}&apiKey=${API_KEY}`);
await dispatch(fetchAPI(res.data.articles));
}
catch(error) {
console.log(error);
}
}
export const fetchAPI = (response: ApiReducerInterface[]) => ({
type: FETCH_API,
articles: response,
});
这是我用于获取主题的按钮:
import { useState } from 'react';
import { searchTopics } from '../../store/apiReducer/action';
const Search: React.FC = () => {
const [search, setSearch] = useState('');
return(
<div>
<input onChange={(e) => setSearch(e.target.value)} placeholder="Search for a topic ..."/>
<button onClick={searchTopics(search)}>Submit</button>
</div>
);
}
export default Search;
如果有人有什么想法可以帮助我吗? :D
感谢您考虑我的请求并愉快地编码:)
I am currently working on a personal project. I use an API to fetch news articles : I fetch latest news when the APP is rendering, not a single issue, the store gets all the datas needed and so on.
But when I start want to search for a topic, nothing happens and I have this error in the browser console : "TypeError: dispatch is not a function"
Here's my action file where the error occurs
import { API_URL_BASE, API_KEY, FETCH_API } from "./constants";
import { ApiReducerInterface } from "./apiReducerInterface";
import axios from "axios";
export const fetchMainTopics = () => async (dispatch: any) => {
try{
const res = await axios.get(`${API_URL_BASE}top-headlines?country=fr&apiKey=${API_KEY}`);
await dispatch(fetchAPI(res.data.articles));
}
catch(error) {
console.log(error);
}
}
export const searchTopics = (search: string) => async (dispatch: any) => {
try{
const res = await axios.get(`${API_URL_BASE}everything?q=${search}&apiKey=${API_KEY}`);
await dispatch(fetchAPI(res.data.articles));
}
catch(error) {
console.log(error);
}
}
export const fetchAPI = (response: ApiReducerInterface[]) => ({
type: FETCH_API,
articles: response,
});
Here's my button to fetch the topic :
import { useState } from 'react';
import { searchTopics } from '../../store/apiReducer/action';
const Search: React.FC = () => {
const [search, setSearch] = useState('');
return(
<div>
<input onChange={(e) => setSearch(e.target.value)} placeholder="Search for a topic ..."/>
<button onClick={searchTopics(search)}>Submit</button>
</div>
);
}
export default Search;
If anyone has an idea to help me out there please ? :D
Thanks for considering my request and happy coding :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论