状态不更新并出现错误:TypeError:调度不是函数

发布于 2025-01-15 08:28:20 字数 1601 浏览 2 评论 0原文

我目前正在从事一个个人项目。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文