React Native:函数未定义
React Native: Function Undefined
我正在用 React Native 构建一个应用程序,但在定义函数时一直出现错误,似乎无法弄清楚如何修复它。
这是导致错误的代码部分。它旨在使用 redux
和 redux-thunk
从 api 获取数据。
componentDidMount() {
this.props.fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
}
这是组件使用的 fetchData
函数,它位于 actions 文件夹中。
export const fetchData = url => {
return async dispatch => {
dispatch(fetchingRequest());
try {
let response = await fetch(url);
let json = response.json();
dispatch(fetchingSuccess(json));
} catch(error){
dispatch(fetchingFailure(error));
}
}
};
这是我尝试将其传递给组件的方式
SearchScreen.propTypes = {
fetchData: PropTypes.func.isRequired,
response: PropTypes.object.isRequired
};
propType 只定义类型,不定义实际值。
如果你导出函数
export const fetchData
只需从您的组件文件中导入它,然后使用它:
fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
没有"this.props"。
因此,正如我在 propTypes
的评论中所说,您没有传递任何内容,而是在测试道具的类型。您应该导入您的函数并将其传递给 connect
.
import { connect } from 'react-redux';
import { fetchData } from '../actions';
export default connect(mapStateToProps, { fetchData })(App);
我正在用 React Native 构建一个应用程序,但在定义函数时一直出现错误,似乎无法弄清楚如何修复它。
这是导致错误的代码部分。它旨在使用 redux
和 redux-thunk
从 api 获取数据。
componentDidMount() {
this.props.fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
}
这是组件使用的 fetchData
函数,它位于 actions 文件夹中。
export const fetchData = url => {
return async dispatch => {
dispatch(fetchingRequest());
try {
let response = await fetch(url);
let json = response.json();
dispatch(fetchingSuccess(json));
} catch(error){
dispatch(fetchingFailure(error));
}
}
};
这是我尝试将其传递给组件的方式
SearchScreen.propTypes = {
fetchData: PropTypes.func.isRequired,
response: PropTypes.object.isRequired
};
propType 只定义类型,不定义实际值。
如果你导出函数
export const fetchData
只需从您的组件文件中导入它,然后使用它:
fetchData(
V.sprintf(
"http://timetree.igem.temple.edu/api/pairwise/%s/%s",
this.state.taxonA,
this.state.taxonB
)
);
没有"this.props"。
因此,正如我在 propTypes
的评论中所说,您没有传递任何内容,而是在测试道具的类型。您应该导入您的函数并将其传递给 connect
.
import { connect } from 'react-redux';
import { fetchData } from '../actions';
export default connect(mapStateToProps, { fetchData })(App);