Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

React 18: TS2786: 'Rating' cannot be used as a JSX component. #161

Open
mleister97 opened this issue Nov 17, 2022 · 9 comments
Open

React 18: TS2786: 'Rating' cannot be used as a JSX component. #161

mleister97 opened this issue Nov 17, 2022 · 9 comments

Comments

@mleister97
Copy link

After upgrading to React 18.2.0 i get the following error when using the Rating-Component

TS2786: 'Rating' cannot be used as a JSX component.   Its instance type 'Rating' is not a valid JSX element.     The types returned by 'render()' are incompatible between these types.       Type 'import("/node_modules/react-rating/node_modules/@types/react/index").ReactNode' is not assignable to type 'React.ReactNode'.

Please fix for all typescript users!

@mleister97 mleister97 changed the title TS2786: 'Rating' cannot be used as a JSX component. React 18: TS2786: 'Rating' cannot be used as a JSX component. Nov 17, 2022
@hanieljazzar
Copy link

hanieljazzar commented Dec 23, 2022

try wrapping the component in a new component

               
               import { Star } from "react-feather";
                import Rating from "react-rating";
                
                const RatingComponent = ({ initialRating = 0, readonly = false, onChange = null, showCount = false, showBy = false }) => {
                    return (
                        <>
                            <div className=" mb-1">
                                <Rating
                                    readonly={readonly}
                                    initialRating={initialRating}
                                    emptySymbol={<Star size={32} fill={readonly ? "#FFF" : '#DEDFE3'} stroke={readonly ? "#FFF" : '#DEDFE3'} />}
                                    fullSymbol={<Star size={32} fill="#FC8264" stroke="#FC8264" />}
                                    onChange={onChange}
                                />
                            </div>
                        </>
                    )
                }
                
                export default RatingComponent
      

@adham618
Copy link

@ dreyescat you need to update react types to 18
https://stackoverflow.com/questions/71841181/some-components-cannot-be-used-as-a-jsx-component

@zguo123
Copy link

zguo123 commented Feb 27, 2023

@adham618 @hanieljazzar Unfortunately, your solutions do not solve the problem. It still shows the same errors. It's up to the developers to update their components to be usable with React 18

@rdtawagon
Copy link

Did anyone solve this issue? Having the same problem on react typescript

@adham618
Copy link

adham618 commented Mar 7, 2023

@rdtawagon
delete node_modules and yarn.lock
then add this to the package.json
"resolutions": {
"@types/react": "^18.0.2",
"@types/react-dom": "18.0.2"
}

@hardikwebiots
Copy link

hardikwebiots commented Mar 25, 2023

@rdtawagon delete node_modules and yarn.lock then add this to the package.json "resolutions": { "@types/react": "^18.0.2", "@types/react-dom": "18.0.2" }

This solution did not resolve my issue do you have any other solution?

@bauerbach
Copy link

The actual problem is that this project is dead.

@ThaheemDev
Copy link

// Rating.jsx
import React from "react";
import Rating from "react-rating";

const NewRating = ({ ...props }) => <Rating {...props} />;
export default NewRating;

image

// Rating.d.ts
import { FC } from "react";
import { RatingComponentProps } from "react-rating";

declare const NewRating: FC;

export default NewRating;

image image

You can copy and paste this code into a file named Rating.jsx in your project's desired directory. This file will contain both the JSX component (NewRating) and the corresponding declaration file (Rating.d.ts). Make sure that the react-rating package is installed in your project before using this component.

@yakovenkoroman1993
Copy link

I suggest such work around for TS

import React from "react";
import ReactRating, { RatingComponentProps } from "react-rating";

type Props = RatingComponentProps;

export default function Rating(props: Props) {
  const Root = ReactRating as unknown as ((props: Props) => JSX.Element);

  return (
    <Root {...props} />
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants