Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Type 'void' is not assignable to type 'ReactNode'

I'm trying to get an error while calling the function, this is just for practice so I have kept everything inside App.tsx. My class looks like this:

enum Actor {
  None = '',
}

const initializeCard = () => {
  //some logic here
  return card;
}

export default class App extends Component<{}, State> {
  state: State ={
    card: initializeCard()
  }

  public renderRows = () => {
    const { card } = this.state;
    board.map((actor, index) => this.renderRow(actor, index));
  }

  public renderRow = (actor: Actor, index: number) => {
    return(
      <div className="cell">

      </div>
    )
  }

  public render() {
    return (
      <div className="App">
        <div>
            { this.renderRows() } // Here I'm getting the error
        </div>
      </div>
    )
  }
}

My Package.json looks like:

"dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.3.2",
    "@testing-library/user-event": "^7.1.2",
    "@types/jest": "^24.0.0",
    "@types/node": "^12.0.0",
    "@types/react": "^16.9.0",
    "@types/react-dom": "^16.9.0",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-scripts": "3.4.1",
    "typescript": "~3.7.2"
  }

The complete error is as following:

Type 'void' is not assignable to type 'ReactNode'.ts(2322) index.d.ts(1348, 9): The expected type comes from property 'children' which is declared here on type 'DetailedHTMLProps, HTMLDivElement>'

I've searched for the solutions for this error and found this but I couldn't solve my problem with this solution. Please let me know how can I fix this. Thanks.

like image 256
user1547554 Avatar asked Apr 09 '20 23:04

user1547554


People also ask

Is not assignable to type Reactnode undefined?

The "Type 'undefined' is not assignable to type" error occurs when a possibly undefined value is assigned to something that expects a different type. To solve the error, use the non-null assertion operator or a type guard to verify the value is of the specific type before the assignment.

Is not assignable to type () => void?

The "Type 'void' is not assignable to type" TypeScript error occurs when we forget to return a value from a function, so the function gets an implicit return type of void . To solve the error, make sure you return a value of the correct type from your functions before the assignment.

What is Reactnode?

A React node is defined as: a light, stateless, immutable, virtual representation of a DOM node. React nodes are not real DOM nodes (e.g., text or element nodes) themselves, but a representation of a potential DOM node. The representation is considered the virtual DOM.

What is IntrinsicAttributes?

IntrinsicAttributes interface can be used to specify extra properties used by the JSX framework which are not generally used by the components' props or arguments - for instance key in React. Specializing further, the generic JSX.


1 Answers

You need to return something from your function.

Where is renderCell function? perhaps you mean renderRow?

like image 183
Esther-I Avatar answered Sep 21 '22 09:09

Esther-I