skip to Main Content

I am working on a project that uses react. I have added another component and I want to pass information from the child component to the parent. the problem is that when I try to use the useState hook it all components disappear from the browser and the app renders an empty root div.

I have noticed that react is being imported and used using import * as React from 'react'; in the tsx file. I tried to change it to import React, { useState } from 'react'; but it gave errors so I tried using the hook using React.useState() and also tried importing use state as import * as React from 'react'; import { useState } from 'react'; but in all cases using the hook in the file cause all the jsx in the file to not show.

also I tried to use other state management solutions such as redux.

import { useDispatch, useSelector } from "react-redux";

import { decrement, increment, incrementByAmount } from "./redux/counter.js";

const { count } = useSelector((state) => state.counter);

const dispatch = useDispatch();

but it gives the error: Property 'counter' does not exist on type 'unknown'.ts(2339) any.
even though counter exists in the relevant folder.

2

Answers


  1. Can you show me the child and parent code snippets?

    Login or Signup to reply.
  2. It seems like you are facing some issues with importing and using React’s hooks in your TypeScript file
    When you import React using import * as React from ‘react’;, you should use the hook as React.useState(). If you want to use the more concise syntax, you can change the import to import React, { useState } from ‘react’; like you tried. To resolve the issue with this import, make sure you are using TypeScript version 2.7 or higher, as this syntax was introduced in TypeScript 2.7.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search