简体   繁体   English

React - 绑定参数时不会触发子 onClick 事件

[英]React - Child onClick event doesn't trigger when binding parameters

My problem is the following.我的问题如下。

  • I have a "BookShelf" component which contains a "Book" list.我有一个包含“Book”列表的“BookShelf”组件。
  • The parent (bookshelf) manage in its state a "selectedBook" property.父(书架)在其状态下管理“selectedBook”属性。
  • When clicking on one child (book), I would like to update its parent selectedBook property.单击一个孩子(书)时,我想更新其父selectedBook属性。
  • To achieve this, I use a function defined in the parent properties.为了实现这一点,我使用了在父属性中定义的函数。
  • But this method is never trigerred (I tried with a console.log('something') but it never shows.但是这种方法永远不会触发(我尝试使用 console.log('something') 但它从未显示。

See my code below :请参阅下面的代码:

setSelectedBook(index) {
    this.setState({
        selectedBook: index
    })
},
getInitialState() {
    return {
        books: [],
        selectedBook: null
    }
},
componentDidMount() {
    let component = this
    $.ajax({
        url: 'data/books.json',
        success (data) {
            component.setState({
                books: data
            })
        },
        error (err) {
            console.log(err)
        }
    })
},
render() {
    let component = this
    var bookList = this.state.books.map(function(book, index) {
        let selectBook = component.setSelectedBook.bind(component, index)
        return (
             <Book onClick={selectBook} data={book} key={index} />
         )
    })
    return <div className="book-shelf">
        {bookList}
    </div>
}

Thanks in advance !提前致谢!

Here is a simple example for you.这是一个简单的例子。 Also fiddle还有fiddle

You should pass your onClick event as a props to child component, once child component gets it, it will call a callback and pass an id as an agrument to the callback (like i have below).您应该将您的onClick事件作为道具传递给子组件,一旦子组件获取它,它将调用回调并将 id 作为参数传递给回调(如下所示)。

class Book extends React.Component {
    constructor(props){
    super(props);
    this.handleClick = this.handleClick.bind(this);
  }
  handleClick(){
    this.props.click(this.props.id)
  }
    render(){
    return  <li onClick={this.handleClick}>{this.props.id} - {this.props.name}</li>
  }
}

class BookShelf extends React.Component{
    constructor(){
    super();
    this.onClick = this.onClick.bind(this)
  }
  onClick(id){
    console.log(id)
  }
  render(){
    return <ul> // of course you may use Array.map functions, it's just for example
      <Book click={this.onClick} id={1} name={'hello'}/>
      <Book click={this.onClick} id={2} name={'world'}/>
    </ul>
  }
}

React.render(<BookShelf />, document.getElementById('container'));

Also i suggest look at this article Communicate Between Components , it will be useful for you.另外我建议看看这篇文章在组件之间通信,它对你很有用。

Thanks谢谢

select method return anonymous function as value. select 方法返回匿名函数作为值。

<Book onClick={this.selectBook(index)} data={book} key={index} />

 selectBook (index){ return ((() => { console.log(" selectBook fired" ); component.setState({selectedBook:index}); }).bind(component,index)) }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM