0

Hello fellow coding friends,

I have a small problem. First of all I am very new to REACT and Typescript otherwise I think it would be no problem to solve this myself. But i need the following code to be implemented as a class (as I otherwise would need to duplicate this code):

const CustomFilterComponent = (props): ReactElement => {
const [selectedVal, setSelectedVal] = useState(0);

function handleChange(e): void {
    const val = e.target.value;
    setSelectedVal(val);
    props.onFilterChanged(props.columnDef.tableData.id, val);
}

return (
    <th>
        <Select  value={selectedVal} onChange={handleChange}>
            <MenuItem value={"0"} disabled>
                Filter
            </MenuItem>
            {phpVersions.map((phpVersion): ReactElement => {
                return (
                    <MenuItem value={phpVersion}>{phpVersion}</MenuItem>
                )
            })}
        </Select>
    </th>
);
};

I need this to be a class as I am making a filter for php versions in my example. But I also need this for symfony as a reference. This means I would need to change the return property. At the moment i call my method like this:

const newColumns = [...this.state.columns];
newColumns[3].filterComponent = CustomFilterComponent;
this.setState({columns: newColumns});

I would very much appreciate your help.

3
  • so what do you actually need just converting above functional component to the class component? Commented Aug 10, 2020 at 6:09
  • Yes. But my knowledge currently is just not enough Commented Aug 10, 2020 at 6:10
  • I dropped the basic conversion for you Commented Aug 10, 2020 at 6:19

1 Answer 1

1

Here's the simple class component for above functional component:

import React from "react";

interface ColumnDef {
  tableData: {
    id: number
  }
}

interface Props {
  columnDef: ColumnDef
  onFilterChanged: (prev: number, next: number) => void
}

interface State {
  selectedVal: number
}

class CustomFilterComponent extends React.Component<Props, State> {
  state = {
    selectedVal: 0,
  }
  
  handleChange = e => {
    const val = e.target.value;
    this.setState(val);
    this.props.onFilterChanged(this.props.columnDef.tableData.id, val);
  }
  
  render() {
    return (
      <th>
        <Select value={this.state.selectedVal} onChange={this.handleChange}>
            <MenuItem value={"0"} disabled>
                Filter
            </MenuItem>

            {/* What is phpVersions? */}
            {phpVersions.map((phpVersion): ReactElement => {
              return (
                <MenuItem value={phpVersion}>{phpVersion}</MenuItem>
              )
            })}
        </Select>
      </th>
    );
  }
}

Note: I aslo commented what phpVersions comes from as well? If that is a part of props, just simply change to: this.props.phpVersions

Sign up to request clarification or add additional context in comments.

6 Comments

php Version is actually just an array. but it is not defined in props.
this.props.onFilterChanged(this.props.columnDef.tableData.id, val); - this line is not working for me. I get the following error: "onFilterChanged does not exist on type...".
Ah I forgot you using Typescript? If so, we have to define props/state to pass to component as typings
I switched the answer as Typescript style
BTW I don't know why you put the logic of rendering phpVersions into this component as this property doesn't belong to the component. You might have to check again though.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.