I have composite components. The parent component should be able to pass props to child components when clicked.
When I add the onClick handler to the search component of my header, there are no messages printed to the console.
Parent component header
import React from "react"
import styled from "styled-components"
import Search from "./search"
const Container = styled.header``
const Wrapper = styled.div``
function Header () {
function toggleSearch(e) {
e.preventDefault();
console.log('Search')
}
return (
<Container>
<Wrapper>
<Search onClick={toggleSearch} />
</Wrapper>
</Container>
)
}
export default Header
and the child component search
import React from 'react'
import styled from 'styled-components'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { faSearch } from '@fortawesome/free-solid-svg-icons'
const Container = styled.span``
const Icon = styled(FontAwesomeIcon)
const Search = () => {
return (
<Container>
<Icon icon={faSearch} />
</Container>
)
}
export default Search
- When I add a button to the Header, the click event fires.
- When I change the Search container to a button instead of a span then the button can be clicked but the message is not written to the console.
How do I get my click event to fire?
I am using
"react": "^16.9.0"
"styled-components": "^4.3.2