table icon indicating copy to clipboard operation
table copied to clipboard

onExpandRow event

Open rachel-singh opened this issue 9 years ago • 5 comments

Problem Description

when I add the following to ReactTable onExpandRow={(index, event) => { console.log("expanded"); }} The row does not expand.

Adding an event handler stops the row from being expanded. If I remove the handler it works just fine. What am I missing?

System Information

Chrome Browser ReactTable version 5.5.0

rachel-singh avatar Apr 12 '17 19:04 rachel-singh

onExpandRow is one of many props that helps with using react-table as a fully controlled component. When you pass the onExpandRow prop, it expects you to store the information it provides in your own state (or even change it in some way) and pass it back to the table in the expandedRows prop. Normally, you wouldn't want to use these two props unless you wanted to control the expandedRows state on your own, since the table does this for you automatically.

If you simply want to know if a row has been expanded, but not change the expandedRows state, we would need to look into adding a way for that event to be fired somewhere else.

tannerlinsley avatar Apr 12 '17 19:04 tannerlinsley

I am looking to load data for subcomponent on demand...only when it is expanded. Any suggestions on how I can do that? Tx.

rachel-singh avatar Apr 12 '17 19:04 rachel-singh

subComponents aren't rendered until you click the down arrow, so you can actually do this with the subComponent's lifecycle hooks:

class MySubComponent extends React.Component {
  constructor () {
    super()
    this.state = {
      todos: null
    }
  }
  componentWillMount () {
    axios.get('https://myAwesomeApi.com/todos')
      .then(todos => {
        this.setState({ todos })
      })
  }
  render () {
    const {
      todos
    } = this.state

    return (
      <div>
        <h3>Todos</h3>
        {todos ? (
          <ul>
            {todos.map(todo => (
              <li
                key={todo.id}
              >
                {todo.text}
              </li>
            ))}
          </ul>
        ) : (
          <span>Loading...</span>
        )}
      </div>
    )
  }
}

tannerlinsley avatar Apr 12 '17 19:04 tannerlinsley

I see. Let me give that a try. Tx.

rachel-singh avatar Apr 12 '17 19:04 rachel-singh

Worked like a charm. Closing issue.

rachel-singh avatar Apr 12 '17 21:04 rachel-singh