Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
195
rated 0 times [  196] [ 1]  / answers: 1 / hits: 26875  / 3 Years ago, fri, february 12, 2021, 12:00:00

I am getting this error when I am just trying to pass props around. Very basic stuff.


interface Props extends Omit<React.HTMLAttributes<HTMLElement>, 'color'>, ChakraProps {
handleMoveAll: () => void
}

export const MyWrapper: FC<Props> = (props): JSX.Element => (
<div {...props}>
<IconBox label="Move All" onClick={props.handleMoveAll}>
<MyIcon />
</IconBox>
</div>
)

And then I call the component MyWrapper on its parent where the warning is coming from.


export const ParentComp: FC<{}> = (): JSX.Element => {
const myFunction = () => console.log('Hit it')

return (
<MyWrapper handleMoveAll={() => myFunction()}>
<p>Child Component</p>
</MyWrapper>
)
}


Warning: React does not recognize the handleMoveAll prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase handlemoveall instead. If you accidentally passed it from a parent component, remove it from the DOM element.



If I remove this handleMoveAll={() => myFunction()} the warning is gone.


Any ideas?


More From » reactjs

 Answers
14

The problem is <div {...props}> in MyWrapper; that adds the handleMoveAll prop onto the div. What you could do is something like:


export const MyWrapper: FC<Props> = (props): JSX.Element => (
const {handleMoveAll, ...rest} = props;
<div {...rest}>
<IconBox label="Move All" onClick={handleMoveAll}>
<MyIcon />
</IconBox>
</div>
)

to pull out handleMoveAll from the passed-in props, while still passing any other props into the div.


[#50403] Wednesday, January 27, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tierra

Total Points: 705
Total Questions: 85
Total Answers: 96

Location: Maldives
Member since Sat, Jan 29, 2022
2 Years ago
;