【问题标题】:Getting type error :this.props.getMovieTree is not a function获取类型错误:this.props.getMovieTree 不是函数
【发布时间】:2019-07-29 13:24:12
【问题描述】:

我是 react native 和 redux 的新手,试图调用一个函数,但它给了我一个错误:

 this.prop.getMovieTree is undefined.

当我双击 getmovietree 时,它​​会将我重定向到正确的功能。但它在运行时出错并导致我的应用程序崩溃。当我 console.log(this.props) 它打印定义的值但是当我打印 console.log(this.props.getMovieTree()) 这打印未定义。

// 在 App.js 中:

import {getMovieTree} from './src/actions/RestActions'
export class App extends Component {
  static propTypes = {
    getMovieTree: PropTypes.func,
  };
  constructor(props) {
    super(props)
   }
componentWillMount()
{
this.props.getMovieTree();//at this line getting error: getmovietree() is undefined

}
}
const mapStateToProps = state => {
  const { movieList } = state;

  return {movieList};
};
export default connect(mapStateToProps,getMovieTree )(App);

// 在 RestAction.js 文件中:

import {MAGENTO_GET_MOVIE_TREE} from './types'
import {magento} from '../magento'
  export const getMovieTree = () => {
  return async dispatch => {
    try {
      const customer = await magento.getMovieTreeCall();
      dispatch({
        type: MAGENTO_GET_MOVIE_TREE,
        payload: customer
      });
    } catch (error) {
      console.log(error);
    }
  };
};

// 在 Index.js 中:

export default magento => {
return{
    getMovieTreeCall : () => {
        return new Promise((resolve, reject) => {
         // const path = '/V1/AppVersion/';

          magento
            .send()
            .then(data => {
              resolve(data);

            })
            .catch(e => {
              console.log(e);
              reject(e);
            });
        });
      },
};


};



In magento/index.js:




class Magento {

    send(){

        fetch("https://api.themoviedb.org/3/movie/now_playing?api_key=55957fcf3ba81b137f8fc01ac5a31fb5&language=en-US")
        .then(response => response.json())
        .then((responseJson) => {
            return response.json()
            // this.setState({
            //     loading: false,
            //     dataSource: responseJson.results
            // })
        })
        .catch(error => console.log(error))
    }


}

export const magento = new Magento();

【问题讨论】:

    标签: javascript react-native redux typeerror


    【解决方案1】:
    • 首先,在 Magento 类中,您应该使用 await 或 Promise 来处理异步任务。方法应该是这样的:
    异步发送(){ 等待获取(“https://api.themoviedb.org/3/movie/now_playing?api_key=55957fcf3ba81b137f8fc01ac5a31fb5&language=en-US”) .then(响应 => response.json()) .then((responseJson) => { 返回响应.json() // this.setState({ // 加载:假, // 数据源:responseJson.results // }) }) .catch(错误=> console.log(错误)) } }
    • 那么你应该将 dispatch 映射到 props ,然后将它连接到组件:
        const mapStateToProps = state => ({ getMovieTree: state.getMovieTree })
        //OR
        const mapDispatchToProps = dispatch => {
            return {
                getMovieTree: () => dispatch({  }) ///your desired action
            }
        }
    
        export default connect(mapStateToProps, mapDispatchToProps)(App); 
    

    在这种情况下,我们不需要 mapDispatchToProps,您可以轻松地从 props 调用 getMovieTree。 connect() 的第二个参数是 mapDispatchProps,可以像这样未定义。

    export default connect(mapStateToProps)(App);
    

    【讨论】:

    • 感谢您的回复,但我在 componentwillmount 函数中遇到错误:componentWillMount() { this.props.getMovieTree();//在这一行出现错误:getmovietree() is undefined } 你能告诉我这里有什么问题?
    • 我刚才提到你没有正确地将状态映射到道具。您已将 movieList 道具发送到组件。这可能会导致未定义的道具错误!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-22
    • 2018-08-06
    • 2022-09-24
    • 2022-11-19
    • 1970-01-01
    • 2016-02-26
    • 2013-04-01
    相关资源
    最近更新 更多