首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:函数不是React本机中的函数

TypeError:函数不是React本机中的函数
EN

Stack Overflow用户
提问于 2020-02-22 06:30:07
回答 2查看 102关注 0票数 0

完整错误为TypeError: doFollow is not a function. (In 'doFollow(index), 'doFollow is undefined)

我是React Native的新手,所以我不太确定问题可能是什么,代码如下:

代码语言:javascript
复制
render(){
      const followReq = this.props.navigation.getParam('followRequest', '0')
      const doFollow = this.props.navigation.getParam('doFollow', '')
    return (
      <View style={styles.container}>
        {
            followReq.map((frn, index) =>  (
                <Button 
                    key={frn}
                    title={`Follow ${frn}`}
                    onPress={() => doFollow(index)}
                />
            ))
        }
      </View> 
    );
  }
EN

回答 2

Stack Overflow用户

发布于 2020-02-22 12:24:16

代码语言:javascript
复制
render(){
  const followReq = this.props.navigation.getParam('followRequest', '0')
  const doFollow = this.props.navigation.getParam('doFollow', '')
 return (
   <View style={styles.container}>
       { followReq.map((frn, index) =>  (
            <Button 
                key={frn}
                title={`Follow ${frn}`}
                onPress={() => {
                  this.doFollow(index);}}/>)) }
   </View>);}
票数 0
EN

Stack Overflow用户

发布于 2020-02-22 20:31:09

如下所示访问您的导航参数。

代码语言:javascript
复制
render(){
      const followReq = this.props.navigation.state.params.followRequest;
      const doFollow = this.props.navigation.state.params.doFollow;
    return (
      <View style={styles.container}>
        {
            followReq.map((frn, index) =>  (
                <Button 
                    key={frn}
                    title={`Follow ${frn}`}
                    onPress={() => doFollow(index)}
                />
            ))
        }
      </View> 
    );
  }

或者将导航参数绑定到您的应用程序状态,如果您的应用程序使用react-redux,则如下所示进行访问

代码语言:javascript
复制
import { connect } from 'react-redux';

render(){
      const followReq = this.props.followRequest;
      const doFollow = this.props.doFollow;
    return (
      <View style={styles.container}>
        {
            followReq.map((frn, index) =>  (
                <Button 
                    key={frn}
                    title={`Follow ${frn}`}
                    onPress={() => doFollow(index)}
                />
            ))
        }
      </View> 
    );
  }


const mapStateToProps = (state, props) => {
  return {
    ...props.navigation.state.params
  };
};

export default connect(mapStateToProps)(ComponentName);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60347035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档