在父Browse.js中

import { usersFetch, onToggleFollow } from '../actions';

class Browse extends Component {
  render() {
    return (
      <ListView
       ....
        renderRow={(user) =>
          <UserItem user={user}
            onPress={() => this.props.onToggleFollow()}
          />}
      />
    );
  }
}

export default connect(mapStateToProps, { usersFetch, onToggleFollow })(Browse);


在子UserItem.js中

class UserItem extends Component {

  render() {
    followButton = <Button title='Follow' onPress={() => {
      this.props.onToggleFollow();
    }}/>;

    return (
      <View style={styles.cardContainerStyle}>
        <View>
          { followButton }
        </View>
      </View>
    );
  }
}


在动作创建者文件中:

export const onToggleFollow = () => {
  console.log('onToggleFollow method!');

  return (dispatch) => {
    dispatch({ type: ON_TOGGLE_SUCCESS });
  };
};


我收到一个错误消息:UserItem.js中的“ this.props.onToggleFollow不是函数,this.props.onToggleFollow未定义”,有任何帮助!

最佳答案

您的问题在这条线上:

onPress={() => { this.props.onToggleFollow(); }}


UserItem中。您以onPress属性传递了函数,因此您应该改为调用this.props.onPress

10-05 21:04