score:7
getinitialstate
- the object
**getinitialstate()**
invoked once before the component is mounted. the return value will be used as the initial value of this.state.
note: this method is not available on es6 class components that extend react.component. for more information, please read our documentation about es6 classes.
getdefaultprops
- the object
**getdefaultprops()**
invoked once and cached when the class is created. values in the mapping will be set on this.props if that prop is not specified by the parent component (i.e. using an in check). - this method is invoked before any instances are created and thus
cannot rely on this.props. in addition, be aware that any complex
objects returned by
getdefaultprops()
will be shared across instances, not copied.
score:-3
getdefaultprops()
is for properties.
getinitialstate()
is for states.
score:5
getdefaultprops is for default props,if you don't inject this prop ,it will work .
getinitialstate is for ini state,before the component is mounted.
in fact, what matter is the difference between props and state ,once you understand their differences, their default value's defference is easy to understand.
Source: stackoverflow.com
Related Query
- what is the difference between getDefaultProps and getInitialState react js
- What is the difference between React Native and React?
- What is the difference between using constructor vs getInitialState in React / React Native?
- What is the difference between NextJs and Create React App
- What is the difference between hashHistory and browserHistory in react router?
- React Native - What is the difference between StyleSheet.absoluteFill() and StyleSheet.absoluteFillObject()?
- What is the main difference between React Query and Redux?
- React - What is the difference between renderToString and renderToStaticMarkup
- React Hooks: What is the difference between 'useMutationEffect' and 'useLayoutEffect'?
- What is the difference between React component instance property and state property?
- What is the difference between React component and React component instance?
- How does React router works and what is the difference between <link> and<Route>
- What is the difference between arrow functions and regular functions inside React functional components (no longer using class components)?
- ES6 React - What are the difference between reference, shallow copy and deep copy and how to compare them?
- What is the difference between useHistory() and props.history in React Route
- React Transition Group: What is the difference between the appear, enter, exit events and the enter, active done className suffixes?
- What is the difference between accessible, accessibilityLabel and accessibilityHint properties of Text component in react native?
- What is the real difference between value and defaultValue in React JS?
- What is the difference between a javascript package, node package, and react package?
- What is the difference between React and Preact diff algorithm in depth
- What is the difference between a fibre object in React 16 and a React Element?
- What is the difference between Routing in React and Express
- What is the difference between passing a function name in onclick react and calling it through callback
- What is the difference between key and id in React component?
- what is the difference between React setState and Hooks setState?
- what is the difference between element attribute and component attribute in React <Route><Route/> tag
- What is the difference between function and functional React component?
- What is the difference between Nav.Link vs Link vs NavLink in react router DOM and react bootstrap?
- In React, what is the difference between using functional component and just a function, which returns React node?
- What is the difference between this.function and function call in React
More Query from same tag
- How to avoid ${props => props.myProp} in styled-components?
- NextJs 'npm build' and 'npm start' messes up styles
- When using React-router, I want to pass location of Link to Route
- immutable js in react redux application
- Is it possible to extract the name of a nested arrays?
- List localStorage data
- split string in javascript, reactjs
- React : how to update states which have both mutable and immutable values
- No index signature with a parameter of type 'string' was found on type DataType
- Unable to access state from render return call - 'Cannot read property 'instructionList' of null'
- Rendering ReactJS with CSS
- How can I map an array of dicts inside a React return statement?
- I created a button dynamic with the functional components, how I can get style and size from users as props
- React props states and Redux
- React: CSS not recalculating heights in grid-container when state changes
- How do I retain focus when the user clicks a menu?
- React.js - use svg linear gradient not working
- Axios hitting No 'Access-Control-Allow-Origin' header is present on the requested resource error
- Material UI - DataGrid menu colors
- React-admin TextField in List component shows "null". Can I convert this to blank?
- React - Deleting from middle of list removes last element instead
- How to ignore a jest mock after the first test when testing react reducer
- redux mapDispatchToProps with chained arrow functions
- How to correctly make an Observable.ajax.post request in RXJS5
- HTML get the inner html tags value onSubmit of the form in React
- Should I wrap every prop with useCallback or useMemo, when to use this hooks?
- Testing if a method passed as a props is called on Click
- Material UI CardMedia is not showing images
- How to change fill color for React icons?
- Easy way to set default state values in React JS component