简体   繁体   English

类型错误:this.props.searchMovie (Redux Action) 不是函数

[英]TypeError: this.props.searchMovie (Redux Action) is not a function

Am new in React.js , and am facing an error in Redux when I call it as a prop with the help of mapStateToProps .我是React.js ,当我在mapStateToProps的帮助下将其称为道具时,我在 Redux 中遇到了错误。

This is my class这是我的课

import {Component} from "react";
import React from "react";
import {connect} from "react-redux";
import {searchMovie,fetchMovies} from "../actions/searchActions";
export class SearchForm extends Component{


    onChange = e => {
        this.props.searchMovie(e.target.value);
    };

    onSubmit = e => {
        e.preventDefault()
        this.props.fetchMovies(this.props.text)
    }

    render() {
        return(
            <div className="jumbotron jumbotron-fluid mt-5 text-center">
                <div className="container">
                    <h1 className="display-4 mb-3">
                        <i className="fa fa-search" /> Search for a movie
                    </h1>
                    <form id="searchForm" onSubmit={this.onSubmit}>
                        <input
                            type="text"
                            className="form-control"
                            name="searchText"
                            placeholder="Search Movies"
                            onChange={this.onChange}
                        />
                        <button type="submit" className="btn btn-primary btn-bg mt-3">
                            Search
                        </button>
                    </form>
                </div>
            </div>
        )
    }
}

const mapStateToProps = state => ({
    text: state.movies.text
})

export default connect(mapStateToProps,{searchMovie, fetchMovies})(SearchForm)

If you closely look at onChange() , this is where the error comes from , when I try to type in the input, the exception is thrown :如果您仔细查看onChange() ,这就是错误的来源,当我尝试输入输入时,会抛出异常:

 onChange = e => {
            this.props.searchMovie(e.target.value);
        };

Then my Root component is here :然后我的根组件在这里:

import React from 'react';
import '../styles/App.css';
import Navbar from "../components/Navbar";
import Footer from "../components/Footer";
import {Landing} from "./Landing";
import store from "../store/store";
import {Provider} from "react-redux";

function App() {
  return (
        <Provider store={store}>
            <Navbar/>
            <Landing/>
            <Footer/>
        </Provider>
  );
}

export default App;

Then how I made my actions :然后我如何做出我的actions

import {SEARCH_MOVIE} from "./types";


export const searchMovie = text =>  dispatch => {
    dispatch({
        type: SEARCH_MOVIE,
        payload: text
    })
}

Then, the Reducers然后, Reducers

import { SEARCH_MOVIE} from "../actions/types";

const initialState = {

    text: '',
    movies :[],
    loading: false,
    movie:[]
}


export default function (state = initialState, action) {

    switch (action.type) {
        case SEARCH_MOVIE:
            return {
                ...state,
                text: action.payload,
                loading: false
            }
        default:
            return state
    }
}

reducer/index.js , is where I combine my reducers : reducer/index.js ,是我结合我的减速器的地方:

import {combineReducers} from "redux";
import searchReducer from './searchReducer'


export default combineReducers({
    movies:searchReducer
})

Then finally my store :最后是我的store

import {createStore, applyMiddleware} from 'redux'
import thunk from "redux-thunk";
import {composeWithDevTools} from "redux-devtools-extension";
import rootReducer from '../reducers'

const middleware = [thunk]
const initialState = {};

const store = createStore(rootReducer,
    initialState,
    composeWithDevTools(applyMiddleware(...middleware)))

export default store

Can anyone tell me where am wrong ?谁能告诉我哪里错了?

I've never seen the mapDispatchToProps done like you have before.我从未见过像您以前那样完成的 mapDispatchToProps。 Try this:尝试这个:

import { bindActionCreators } from "redux";
import actions from "../actions/searchActions";

const mapDispatchToProps = dispatch => bindActionCreators(actions, dispatch);
export default connect(mapStateToProps,mapDispatchToProps)(SearchForm);

If this doesn't work, you've got a deeper nested problem with the way you have setup redux如果这不起作用,则说明您设置 redux 的方式存在更深层次的嵌套问题

 const searchMovie = text => dispatch => {
    dispatch({
      type: SEARCH_MOVIE,
      payload: text
    });
  };
 export default searchMovie;

Try this in the searchActions file and remove fetch movie functions from everywhere {remove it from the search form in the connect function, the import statement, and onSubmit function } .在 searchActions 文件中试试这个,并从任何地方移除 fetch movie 函数{remove it from the search form in the connect 函数、import 语句和 onSubmit 函数} It will work right now, then open redux from redux extension in the chrome browser and see the state if it changes when you start typing or not.它将立即工作,然后在 chrome 浏览器中从 redux 扩展程序打开 redux 并查看状态是否在您开始输入时发生变化。 and guess what it will change without errors:)并猜测它会发生什么变化而不会出错:)


After that add the fetchmovie function in searchActions file and make sure that you also add it as a second case in the reducer file cause you missed it.之后,在searchActions 文件中添加 fetchmovie 函数,并确保您也将它作为第二种情况添加到 reducer 文件中,因为您错过了它。 Also, add it into the search form as the second parameter in the connect function, the import statement, and in the onSumbit function.此外,将其作为连接函数、import 语句和 onSumbit 函数中的第二个参数添加到搜索表单中。

Add this in searchActions在 searchActions 中添加这个

export const searchMovie = text => dispatch => {
dispatch({
  type: SEARCH_MOVIE,
  payload: text
 });
 };

export const fetchMovie = text => dispatch => {
axios
  .get(`https://www.omdbapi.com/?apikey=${APIKey}&s=${text}`)
  .then(response =>
    dispatch({
      type: FETCH_MOVIES,
      payload: response.data
    })
   )
  .catch(err => console.log(err));
  };

And add this in reducer file并将其添加到减速器文件中

  case FETCH_MOVIES:
            return{
                ...state,
                movies: action.payload,
                loading: false
            };   
        default:
            return state;
    }
   }

after that, it will work.之后,它将起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Redux TypeError this.props.setEmailText 不是函数 - React Redux TypeError this.props.setEmailText is not a function TypeError:_this2.props.taskAdded不是函数redux - TypeError: _this2.props.taskAdded is not a function redux React-Redux TypeError:this.props.getAnimals不是函数 - React-Redux TypeError: this.props.getAnimals is not a function React-redux-控制台给出“ TypeError:props.fetchSearchResults不是函数” - React-redux - Console gives “TypeError: props.fetchSearchResults is not a function” 我的应用程序无法将this.props操作识别为函数(反应-Redux) - My app dont recognize a this.props action as a function (React - Redux) Redux动作道具意外行为 - Redux action props unexpected behavior 未捕获的TypeError:从Redux容器调用函数时,React-Redux应用程序中的this.props。***不是函数 - Uncaught TypeError: this.props.*** is not a function in React-redux app while calling a function from redux container &#39;TypeError: store.dispatch is not a function&#39; redux action testing with jest - 'TypeError: store.dispatch is not a function' redux action testing with jest Redux 动作创建者收到“TypeError: dispatch(...) is not a function”(附上代码)? - Redux action creator getting "TypeError: dispatch(...) is not a function" (code attached)? Redux无法执行分派操作,出现未捕获的typeError,表示这不是函数 - Dispatched action with Redux not working, getting uncaught typeError, saying it's not a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM