0

I'm using Redux with Typescript, and I can't pass data between Components.

EmpList.tsx

function mapStateToProps(){//mapping here}
.... //body of a Component
export default connect(mapStateToProps)(EmpList)

Tool.tsx

import EmpList from './EmpList'
....
private empListTemp: RefObject<EmpList> = createRef()

The problem is that I get an error in the Tool.tsx 'EmpList refers to a value but is being used as a type here'. Now, If I remove connect(mapStateToProps) from export, or put export{mapStateToProps, EmpList} in the EmpList.tsx, I don't have that error anymore, but obviously Redux won't work then.

2 Answers 2

1

You need to add forwardRef: boolean to the options of connect for the ref to have any significant effect - and then you use the type of your unconnected component, since you want to "ref" the "inner component", not the function component wrapper created by connect. You would still use the connected component itself though.

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

3 Comments

I think I didn't understan where to use "typeof".. like this?: private empListTemp: RefObject<typeof EmpList> = createRef() sry, I'm new to all of this, and Redux just got my limited knowledge of React and Ts get even more limited
No typeof. The unconnected component. So not what you are at the moment exporting as the default eport (that is your store-connected component - your original component that you want to access the ref to is actually "wrapped inside another component here"). Do export { EmpList as UnconnectedEmpList }, import that and use this as the generic for the ref.
Oh man, you can't imagine how much you've helped me. Thanks a lot!
0

Use useSelector and useDispatch hook

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.