如何在React Native和Redux中作为道具传递动作?

时间:2017-05-28 22:49:01

标签: javascript reactjs react-native redux

父浏览器中的

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 });
  };
};

我得到一个错误说:“this.props.onToggleFollow不是函数,this.props.onToggleFollow未定义”在UserItem.js中,任何帮助谢谢!

1 个答案:

答案 0 :(得分:1)

您的问题就在这一行:

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

。您将该函数作为onPress道具传递,因此您应该调用this.props.onPress