Skip to content

React component isn't redrawn when a filter is applied #444

@JCShauni

Description

@JCShauni

Hello, I'm having a issue really close to the issue #303 but with something a little different.

Quick description : I use a react-bootstrap-table2 with sorting for my user list. Each user can have "git-like tags" which are rendered by a react component.

image

The "Tag" button on the right allows the user to add or remove a users tag, my state.users is updated and the tag is automatically added to the "tag list" on the left.

But, when a filter is added on the "First name" part and a tag is added, the component is not redrawn, despite the state being correct.
image

Removing the filter make the component redrawn and the tag appears in the tag list.

Code

-package.json

    "react-bootstrap-table-next": "^0.1.15",
    "react-bootstrap-table2-filter": "^0.3.2",
    "react-bootstrap-table2-paginator": "^0.1.1"

-Users.jsx


.....
class Users extends React.Component {
constructor(props) {
    super(props);

    const self = this;

    this.state = {
      users: null,
      tags: null
    };
}

function nameColumnFormatter(cell, row, rowIndex, formatExtraData) {
      const firstName = !row.isActive
        ? `${row.firstName} (${self.props.t('inactive')})`
        : row.firstName;
      return (
        <div>
          {firstName}
          <TagsList selectedIds={row.tags} tagsList={self.state.tags} />
        </div>
      );
    }

this.columns = [
      {
        dataField: 'firstName',
        text: this.props.t('First name'),
        sort: true,
        filter: textFilter({
          placeholder: this.props.t('column_filter')
        }),
        formatter: nameColumnFormatter
      },

.......


render() {
    return (
      <div className="container">
        <h1 className="h2">
          {this.props.t('Users')}
          <Link to={`/users/new`}>
            <button className="btn btn-primary btn-sm ml-2">{this.props.t('Add')}</button>
          </Link>
        </h1>
        <BootstrapTable
          keyField="id"
          data={this.state.users}
          columns={this.columns}
          striped
          hover
          condensed
          filter={filterFactory()}
          pagination={paginationFactory(this.paginationOptions)}
          classes="table-sm"
          defaultSorted={this.defaultSorted}
        />
      </div>
    );
  }

TagsList.jsx

class TagsList extends React.Component {
  constructor(props) {
    super(props);
  }

  componentWillReceiveProps(nextProps) {
    if (nextProps) {
      console.log(nextProps);
    }
   
  }

  renderTags() {
    const dic = R.indexBy(R.prop('id'), this.props.tagsList || []);

    const liste = R.sortBy(
      R.compose(R.toLower, R.prop('name')),
      (this.props.selectedIds || []).map(x => dic[x])
    );
    const rows = [];
    liste.filter(x => !!x).map(t => {
      const textColor = colorHelper.getTextColorFromHex(t.color);
      rows.push(
        <label className="tags" key={t.id} style={{ color: textColor, backgroundColor: t.color }}>
          {t.name}
        </label>
      );
    });

    return rows;
  }

  render() {
    if (!this.props.selectedIds) return null;
    return <div className="list-tags">{this.renderTags()}</div>;
  }
}

Observations

  • When a tag is added to a user when a filter is NOT PRESENT then the componentWillReceiveProps() of TagsList.jsx triggers and the component is rightly re-drawn.

  • When a tag is added when a filter IS PRESENT then the componentWillReceiveProps() is not triggered and the tag doesn't appears in the tagsList.

  • Removing the text filter entierly will trigger the componentWillReceiveProps() and the tag appears in the list.

Thank you for your work !

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions