Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

252
Vistas
Material UI DataGrid set default selected row in a class?

I'm currently rewriting my datagrid component to us Material UI, all of my components so far are class components (due to project scope). One thing that I am at a standstill on is how to have the DataGrid have the first row selected by default. I already have a way to get the ID Of row and pass it down, but I am unsure how to implement that function in my project. I was looking at this as an example, but it is a functional component. https://material-ui-x.netlify.app/storybook/?path=/story/x-grid-tests-selection--api-pre-selected-rows&globals=measureEnabled:false

the apiRef in DataGrid uses UseGridApiRef with selectRow()/selectRows() . The problem is, that is a functional hook, and cannot be used in a class directly. What would be the best way for me to implement such a function in my current code? I found very minimal documentation on this specific topic with MUI's datagrid.

import React from 'react';
import {DataGrid} from "@mui/x-data-grid";

class UniversalDataGrid extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            ownerDatabaseRows: [],
            ownerDatabaseColumns: this.props.displayColumns,
            selectionModel: [],
        };

    }

    componentDidMount() {
        if (this.props.sourceData !== 0) {
            let rowData = this.props.sourceData;
            this.setState({ownerDatabaseRows: rowData});
        }
    }

    rowSelectionData = (selectedRow) => {
        this.setState({selectionModel: selectedRow.row});
        this.props.onRowClick(selectedRow);         
    };


    render() {
        let headerHeight = this.props.headerHeight === 'default' ? 56 : this.props.headerHeight;
        let rowHeight = this.props.rowHeight !== "default" ? this.props.rowHeight : 56;
        return (
            <div style={{height: 200, width: '100%'}}>
                <DataGrid
                    columns={this.props.displayColumns}
                    rows={this.state.ownerDatabaseRows}
                    onRowClick={this.rowSelectionData}
                    hideFooter={this.props.hideFooter}
                    headerHeight={headerHeight}
                    rowHeight={rowHeight}
                />
            </div>
        );
    };


}

export default UniversalDataGrid;

I can easily pass another prop for the rowID to be selected by default, as there is code that runs wherever this component is called one level higher. I just need a way to use apiRef and UseGridApiRef. I've had very little experience with functional components, so I'm not sure how to proceed in this case.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda