I am working in React Render Props. So, whenever I use Curly Braces in render function and passing as a props of Counter component my clickCounter component is not called. But it is working when I use parenthesis instead of curly braces. So why is this happening? Why curly braces is not working in a arrow function but parenthesis is working.
clickCounter.js
import React from "react";
const ClickCounter = (props) => {
console.log("called");
console.log(props);
return (
<div>
<p>You clicked {props.count} times</p>
<button onClick={props.handleIncrement}>Click me</button>
</div>
);
};
export default ClickCounter;
Counter.js
import React from "react";
class Counter extends React.Component {
state = {
count: 0,
};
handleIncrement = () => {
this.setState((prevState) => {
return { count: prevState.count + 1 };
});
};
render() {
console.log(this.props);
const { render } = this.props;
return render(this.state.count, this.handleIncrement);
}
}
export default Counter;
App.js not working
import React from "react";
import ClickCounter from "./HOC/ClickCounter";
// import HoverCounter from "./HOC/hoverCounter";
import Counter from "./RenderProps/Counter";
function App() {
return (
<div className="App">
{/* <ClickCounter></ClickCounter>
<HoverCounter></HoverCounter> */}
<Counter
render={(a, b) => {
<ClickCounter count={a} handleIncrement={b}></ClickCounter>;
}}
/>
</div>
);
}
export default App;
App.js Working
import React from "react";
import ClickCounter from "./HOC/ClickCounter";
// import HoverCounter from "./HOC/hoverCounter";
import Counter from "./RenderProps/Counter";
function App() {
return (
<div className="App">
{/* <ClickCounter></ClickCounter>
<HoverCounter></HoverCounter> */}
<Counter
render={(a, b) => (
<ClickCounter count={a} handleIncrement={b}></ClickCounter>
)}
/>
</div>
);
}
export default App;
Why there is difference between () and {} ?