This is usually called a functional component, not a hook component.

For the defaultProps you can do it like this:

const Body = ({ counter }) => {
    return (
        <div>
            {counter}
        </div>
    );
}

Body.defaultProps = {
    counter: 0
}
Answer from Arnaud Christ on Stack Overflow
🌐
CodePen
codepen.io › kikiki_kiki › pen › WNbjaMM
React Hooks: default props example.
use Destructuring assignment</p> <div> <CountDown1 /> <CountDown1 initCount={5} /> </div> <pre className="code"> {`function Component({ propName = initialValue } = {}) { return (<>{propName}</>); }`} </pre> </div> <div className="col col-lg-6"> <p className="bold">2. use <code>defaultProps</code> property</p> <div> <CountDown2 /> <CountDown2 initCount={7} /> </div> <pre className="code"> {`function Component({ propName = initialValue }) { return (<>{propName}</>); } Component.defaultProps = { propName: initialValue }; `} </pre> </div> </div> </div> ); }; ReactDOM.render(<App />, document.getElementById('root'));
🌐
GeeksforGeeks
geeksforgeeks.org › reactjs-defaultprops
ReactJS defaultProps | GeeksforGeeks
April 7, 2025 - In React, defaultProps is a static property that can be assigned to a component to specify default values for props. These default values are used when the parent component does not pass a value for a particular prop.
Top answer
1 of 4
8

I don't know whether this is eligible for an answer but all your concerns could be resolved by declaring your default value as a constant in the app. That means;

const Parent = () => {
  const somethingUndefined;

  return (
    <>
      <Child prop={somethingUndefined} />
    </>
  );
};

const Child = ({ prop = {a: 1} }) => {
  const [x, setX] = React.useState(1);

  React.useEffect(() => {
    setX(x + 1);
  }, [prop]);

  return <div>{x}, {prop.a}</div>;
};

You can change the above code to

const Parent = () => {
  const somethingUndefined;

  return (
    <>
      <Child prop={somethingUndefined} />
    </>
  );
};

const defaultPropValue = {a: 1};

const Child = ({ prop = defaultPropValue }) => {
  const [x, setX] = React.useState(1);

  React.useEffect(() => {
    setX(x + 1);
  }, [prop]);

  return <div>{x}, {prop.a}</div>;
};

This will not cause any infinite loops.

The difference bet these two:- In the first, the prop is initialized to a new value ie, {a: 1} and on every state update, this will be a new object (the new object will be in a new memory location), and it invokes the callback again.

In the second, we initialized and assigned the {a: 1} to defaultPropValue which will not change. Then we assigned this defaultPropValue to prop so that on every re-render, the value assigned to the prop will be the same ( or from the same memory location). So it works as expected.

Hope the idea is clear!

2 of 4
0

The useEffect() will run the first time and invoke the setX() then:

  • setX() will update the state of x which will trigger the component to re-render again.
  • prop will receive a new object const Child = ({ prop = {a: 1} }) => {
  • useEffect() will run again and invoke the setX()

the whole process repeats again, This causes an infinite loop.

Instead you could pass a default value to a property and use it in the useEffect() dependencies array

const Parent = () => {
  let somethingUndefined; // babel complains if we use `const` without value

  return (
    <div>
      <Child prop={somethingUndefined} />      
      <Child prop={{ a: 3 }} />
    </div>
  );
};

const Child = ({ prop = {} }) => {
  const { a = 1 } = prop;
  const [x, setX] = React.useState(1);

  React.useEffect(() => {
    setX(x + 1);
  }, [a]);

  return <div>{x}, {a}</div>;
};

ReactDOM.render(<Parent />, document.getElementsByTagName('body')[0]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.11.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.11.0/umd/react-dom.production.min.js"></script>

🌐
Mantine
v7.mantine.dev › theming › default-props
Default props | Mantine
You can use useProps hook to add default props support to any custom component. useProps accepts three arguments: component name (string) – it is used to connect component with theme · defaultProps – default props on component level – these props are used when default props are not defined ...
🌐
React TypeScript Cheatsheets
react-typescript-cheatsheet.netlify.app › typing defaultprops
Typing defaultProps | React TypeScript Cheatsheets
// you can also declare the type of DefaultProps if you choose // e.g. https://github.com/typescript-cheatsheets/react/issues/415#issuecomment-841223219 type GreetProps = { age: number } & typeof defaultProps; const defaultProps = { age: 21, }; const Greet = (props: GreetProps) => { // etc }; Greet.defaultProps = defaultProps;
🌐
LogRocket
blog.logrocket.com › home › a complete guide to react default props
A complete guide to React default props - LogRocket Blog
June 4, 2024 - Cover three ways to implement default props in React and provide default values for props that are not required by the component.
🌐
GitHub
github.com › DefinitelyTyped › DefinitelyTyped › issues › 30695
[react]: defaultProps doesn't work when FunctionalComponent<P> is used · Issue #30695 · DefinitelyTyped/DefinitelyTyped
November 20, 2018 - import React, { FunctionComponent } from 'react' type Props = { onClick: (ev: import('react').MouseEvent<HTMLElement>) => void children: import('react').ReactChild color: 'red' | 'green' } const Button: FunctionComponent<Props> = ({ onClick: handleClick, color, children }) => ( <button style={{ color }} onClick={handleClick}> {children} </button> ) Button.defaultProps = { color: 'red' } const Test = () => ( <> /* $ExpectError */ <Button onClick={() => console.log('clicked')}>Click me</Button> </> ) Related: #29816 microsoft/TypeScript#27425 ·
Author   Hotell
Find elsewhere
🌐
Medium
medium.com › better-programming › 5-recipes-for-setting-default-props-in-react-typescript-b52d8b6a842c
5 Recipes for Setting Default Props in React and TypeScript | by Guillaume Renard | Better Programming
September 28, 2022 - You can set default values for properties in a nested object, no matter the level of nesting. The implementation of the hook itself remains nice and simple: the code doesn’t need to check if the parameters are initialized (they are, always), and it can access nested props directly (we’ve destructured them into separate variables — no need to type props.bounds.max to access max).
🌐
Americanexpress
americanexpress.io › hydra
Seamlessly Combining Hooks and Render Props in React - American Express Technology
November 6, 2018 - Create a function component that uses the custom Hook and calls a render prop. Export the component as default.
🌐
Medium
medium.com › @matanbobi › react-defaultprops-is-dying-whos-the-contender-443c19d9e7f1
React defaultProps is dying, who’s the contender? | by Matan Borenkraout | Medium
March 18, 2026 - In my opinion, the bigger problem ... a default value to a prop isn’t available in a class, the old class components will still have to use the old propTypes way, thus creating another difference between these two types of components. This problem will disappear in time since the use of class components should decline with hooks...
🌐
Docureacten
docureacten.github.io › defaultprops
defaultProps | React.js: Learn Easily with Examples
In such cases, using defaultProps allows you to define default values that your component can fall back on, ensuring that the component functions correctly even if some props are missing.
🌐
Frontend Armory
frontarm.com › james-k-nelson › conditionally-set-default-props
How to conditionally set default props on React components – Frontend Armory
To do so, you simply assign a defaultProps object to the component; when React renders the component, the default props will be used unless the parent overrides them.
🌐
Medium
medium.com › frontendweb › what-is-defaultprops-in-reactjs-and-how-do-you-use-it-66a9c739da89
What is defaultProps in reactjs, and How do you use it? | by Rajdeep Singh | FrontEnd Web | Medium
December 14, 2022 - According to react docs defaultProps can be defined as a property on the component class itself to set the default props for the class.
🌐
freeCodeCamp
freecodecamp.org › news › how-to-use-react-components
How to Use React Components – Props, Default Props, and PropTypes Explained
October 9, 2024 - State: until the introduction of hooks, functional components couldn't hold State. With hooks, you can now use the useState hook to manage state in functional components. Class component can hold state using the this.state property.
🌐
DEV Community
dev.to › bytebodger › default-props-in-react-typescript-2o5o
Default Props in React/TypeScript - DEV Community
July 29, 2020 - Specifically, it will use TypeScript & React. (Umm... OK. I got some learnin' to do.) I've been wanting to get my feet wet in a TS project for awhile. So I've been diving in eagerly. But in the last week-or-so, something really threw me for a loop. To illustrate the issue, I'm gonna take something from a plain-ol' JS component, and convert it into a TS component. The stub of my JS component looks like this: export default function MyJSComponent(props) { return ( <> Here is MyJSComponent:<br/> {props.children} </> ); } MyComponent.propTypes = { requiredString: PropTypes.string.isRequired, requiredNumber: PropTypes.number.isRequired, optionalBoolean: PropTypes.bool, optionalString: PropTypes.string, optionalNumber: PropTypes.number, }; MyComponent.defaultProps = { optionalBoolean: true, optionalString: 'yo', optionalNumber: 42, };
🌐
Delft Stack
delftstack.com › home › howto › react › react default props
How to Set Default Props for Components in React | Delft Stack
February 2, 2024 - Since React version 16.8, you can even implement the state functionality and execute a function on different lifecycles of the component. You can set default props for functional components using the defaultProps property.
🌐
YouTube
youtube.com › watch
React Default Props in Functional Components
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.