Answer the question
In order to leave comments, you need to log in
How to pass in JSX argument in React/Redux with Typescript?
I decided to try using TS for React development and ran into a problem that I didn’t quite understand.
I want to pass JSX code into the action-creator arguments of the modal window (in order to pass different views there as needed), but for some reason TS swears and behaves inappropriately.
He justifies this with this:
[ts] Type "boolean" cannot be assigned to type "Element". [2322]
modal.ts(63, 5): The expected type comes from property 'message' which is declared here on type 'IModalOptions'
export const logout = () => {
return (dispatch: Dispatch<any>) => {
dispatch({
type: AUTH_LOGOUT
});
dispatch(modalOpen({
message: <div>TEST</div>
}));
}
}
interface IModalOptions{
message: JSX.Element;
isCanClose?: boolean
}
export const modalOpen = ({message, isCanClose = true} : IModalOptions) => {
return (dispatch: Dispatch<any>) => {
dispatch({
type: MODAL_OPEN,
message,
isCanClose
});
}
}
[ts] Cannot find name "div". [2304]
Answer the question
In order to leave comments, you need to log in
Found the solution myself. If you change the action file format from .ts to .tsx, then the div appears in its context and can be treated as JSX.Element or React.ReactNode.
message: '<div>TEST</div>' The
quotes will fundamentally have an effect
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question