Javascript 获取类型错误:this.props.getMovieTree不是函数
我不熟悉react native&redux尝试调用函数,但它给了我一个错误:Javascript 获取类型错误:this.props.getMovieTree不是函数,javascript,react-native,redux,typeerror,Javascript,React Native,Redux,Typeerror,我不熟悉react native&redux尝试调用函数,但它给了我一个错误: this.prop.getMovieTree is undefined. 当我双击getmovietree时,它会将我重定向到正确的函数。但它在运行时出错&使我的应用程序崩溃。当我打印console.log(this.props)时,它打印定义的值,但当我打印console.log(this.props.getMovieTree())时,它打印未定义的值 //在App.js中: import {getMovieT
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();
- 首先,在Magento类中,您应该使用wait或Promise来处理异步任务。方法应如下所示:
getMovieTree
。connect()的第二个参数是mapDispatchProps,可以像这样取消定义
export default connect(mapStateToProps)(App);
谢谢你的回答,但是我在componentwillmount()函数中得到错误:{this.props.getMovieTree();//在这一行得到错误:getMovieTree()未定义}你能告诉我这里有什么问题吗?我刚才提到,你没有正确地将状态映射到props。您已将
movieList
prop发送到组件。这可能会导致未定义的道具错误!
const mapStateToProps = state => ({ getMovieTree: state.getMovieTree })
//OR
const mapDispatchToProps = dispatch => {
return {
getMovieTree: () => dispatch({ }) ///your desired action
}
}
export default connect(mapStateToProps, mapDispatchToProps)(App);
export default connect(mapStateToProps)(App);