2017-07-18 46 views
0

我試圖用一個React Bootstrap Button Link調用句柄按鈕鏈接不調用處理程序作出反應

我不知道爲什麼點擊後點擊不調用我的處理程序:

import React, { Component } from 'react' 
import { HelpBlock, Button, Table } from 'react-bootstrap' 

export default class Users extends Component { 
    render() { 
    const { users } = this.props 
    return (<span><UserList list={users} /></span>) 
    } 
} 

export class UserList extends Component { 
    render(){ 
    const { handleResetPassword, list, resetPasswordError } = this.props 
    const userList = list && list.map(
     (user, i) => 
      <User 
      handleResetPassword={handleResetPassword} 
      key={i} 
      resetPasswordError={resetPasswordError} 
      user={user} 
      />) 

    return(<Table responsive> 
     <thead> 
     <tr> 
      <th>First</th> 
      <th>Last</th> 
     </tr> 
     </thead> 
     <tbody>{userList}</tbody> 
    </Table>) 
    } 
} 

export class User extends Component { 
    render() { 
    const { uuid, firstName, lastName, email } = this.props.user 

    return (
     <tr> 
     <td>{firstName}</td> 
     <td>{lastName}</td> 
     <td> 
     <HelpBlock disabled={this.props.resetPasswordError ? true : false}>{this.props.resetPasswordError}</HelpBlock> 
      <Button 
      bsStyle='link' 
      onClick={this.props.handleResetPassword} 
      > 
      reset password 
      </Button></td> 
     </tr> 
    ) 
    } 
} 

我也注意到,在頁面加載時,由於某種原因它自動觸發我的處理程序,我甚至沒有點擊按鈕。我看到被打的console.log("handleResetPassword invoked!") ...不知道爲什麼

UsersContainer (我會重構這個代碼在try/catch語句是這樣的setState FYI消除重複)

import { connect } from 'react-redux' 
import React, { Component } from 'react' 

import * as UserAsyncActions from '../actions/Users/UserAsyncActions' 
import Users from '../components/Users/UserList' 

class UsersContainer extends Component { 
    constructor(props){ 
    super(props) 

    this.state = { 
     resetPasswordError: null 
    } 

    this.handleResetPassword = this.handleResetPassword(this) 
    } 
    async componentDidMount() { 
    await this.props.allUsers(this.props.token) 
    } 

    async handleResetPassword() { 
    // e.preventDefault() 
    console.log("handleResetPassword invoked!") 
    try { 
     await this.props.resetUserPassword() 
     if(this.props.hasResetPassword){ 
     // show successful message (set message here) 
     return 
     } 
    } 
    catch(err) { 
     this.setState({ 
     resetEmailValidationState: 'error', 
     resetPasswordError: !this.state.hasResetPassword && 'reset failed' 
     }) 
    } 

    this.setState({ 
     resetEmailValidationState: 'error', 
     resetPasswordError: !this.state.hasResetPassword && 
     'reset failed' 
    }) 
    } 

    render(){ 
    return (
     <Users 
     handleResetPassword={this.handleResetPassword} 
     resetPasswordError={this.state.resetPasswordError} 
     users={this.props.users} 
     />) 
    } 

} 
export const mapStateToProps = state => ({ 
    isRequestingAllUsers: state.user.isRequestingAllUsers, 
    hasResetPassword: state.user.hasResetPassword, 
    users: state.user.users, 
    token: state.auth.token 
}) 

export const mapDispatchToProps = { 
    allUsers: UserAsyncActions.allUsers, 
    resetUserPassword: UserAsyncActions.resetPasssord 
} 

export { Users } 
export default connect(mapStateToProps, mapDispatchToProps)(UsersContainer) 
+0

爲什麼不使用redux從用戶派發動作而不是浪費時間通過幾個組件來傳遞函數? –

+0

我是,我正在通過thunk動作派遣,但我更願意將我的行爲集中在容器中。我不喜歡把處理程序放在愚蠢的組件階段......這只是它的方式,這是很好的做法,我也是TDD,因此從集裝箱級開始TDD一個新功能是完全合理的。相信我,當你TDD時,你會明白爲什麼......它強制設計好。 – PositiveGuy

+0

好吧,React中的愚蠢組件應該是功能類型。所有這些都可能有基於類的React組件從它們中刪除。 –

回答

0

我認爲你的格式不正確bind()UserContainer,應該是this.handleResetPassword = this.handleResetPassword.bind(this)

+0

我做不想在用戶中使用我的處理邏輯。我將它集中在我的容器中。在我的世界中,行爲保持中心並在容器中定義,並儘可能地保持愚蠢組件的行爲。我也從容器級別開始TDD行爲 – PositiveGuy

+0

handleResetPassword在容器中定義並傳遞,並且我已經綁定處理程序在容器級_this_;看看我的容器的構造函數 – PositiveGuy

+0

當你在這裏寫你的代碼時,這段代碼是否爲'this.handleResetPassword = this.handleResetPassword(this)'? – Kris

相關問題