I have a functional react-typescript component which receives a click handler that is firing correctly, however, the component does not update after click event.
i.e:
// for reasons out of the scope of this question,
// this component should be functional
const Heroes: React.SFC<{}> = () => {
// for fututre reasons, these variables must leave within this component
const thor = "isHero";
const hulk = "isHero";
function appendSpan() {
console.log(thor === hulk);
return thor === hulk ? <span>yes</span> : <span>no</span>;
}
return <p onClick={appendSpan}>Are they both heroes?</p>;
};
function App() {
return (
<div className="App">
<Heroes />
</div>
);
}
If I'd call the method immediately i.e <p>Are they both heroes? {appendSpan()}</p>
The <span> is appended accordingly, However, the same isn't true for the onClick scenario.
Could you please help me understand what I am missing here?
Here's the code sandbox
appendSpanupdates neither. You probably want to pass your callback to your Heroes component so the app can update something and pass the updated something in as a prop to be rendered.