1

I have a component that simply renders a row of data and has a delete button for each row. Clicking delete should simply change the state using a filter to filter out the clicked row. Why do I get the error below.

I tried debugging use the console.log and I am indeed getting the correct row.id and rowId to filter on, but my rows state is not being reassigned.

interface TableSampleProps {
    rows: any[];
}

interface TableSampleState {
    rows: any[];
}

export class TableSample extends React.Component<TableSampleProps, TableSampleState> {
    constructor(props: TableSampleProps) {
        super(props);

        this.state = {
           rows: this.props.rows.concat(),
        };
    }

    public render() {
        return <MyTable rows={this.state.rows} onDeleteRow={this.deleteRow} />;
    }

    private deleteRow = (rowId: number) => {
        // console.log(rowId);
        // this.state.rows.filter((row) => console.log(row.id !== rowId));
        this.setState = {
            rows: this.state.rows.filter((row) => row.id !== rowId),
         };
    }
}

] ERROR in ./src/ts/components/table-sample-tool.tsx [0] (57,13): error TS2322: Type '{ rows: any[]; }' is not assignable to type '{ (f: (prevState: TableSample State, props: TableSampleProps) => Pick, ...'. [0] Object literal may only specify known properties, and 'rows' does not exist in type '{ (f: (prevS tate: TableSampleState, props: TableSampleProps) => Pick, ...'. [0] Child html-webpack-plugin for "index.html": [0] chunk {0} index.html 542 kB [entry] [0]
+ 4 hidden modules [0] webpack: Failed to compile.

2 Answers 2

1

Try using setState({rows=... instead of setState = {rows.... You can do this.state = {} in the constructor but setState is a function.

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

2 Comments

Duh!! Totally overlooked that cause I copied from the constructor. Thanks!
1: Your code is invalid syntax 2: setState is asynchronous. Accessing previous state via this.state inside it does not guarantee you get the previous state @user1991118
0

That is now how you set state! setState is a function, you don't assign it (hence the error), you call it, passing in an object indicating updated properties in state:

this.setState({
    rows: this.state.rows.filter((row) => row.id !== rowId),
});

Another thing to note is that setState is asynchronous so accessing this.state.rows inside it will not guarantee the previous state. Use a callback for setState, the first argument to the callback will be the previous state, and return an object from the callback like so:

this.setState(prevState => ({
    rows: prevState.rows.filter((row) => row.id !== rowId),
}));

This will guarantee that you're accessing previous state.

Comments

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.