The most fundamental component for building UI, View is a container that supports layout with flexbox, style, some touch handling,
and accessibility controls, and is designed to be nested inside other views and to have 0 to many children of any type.
View maps directly to the native view equivalent on whatever platform React is running on,
whether that is a UIView,
If using the new style context, re-declare this in your class to be the
React.ContextType of your static contextType.
Should be used with type annotation or static contextType.
Example
staticcontextType = MyContext // For TS pre-3.7: context!: React.ContextType<typeofMyContext> // For TS 3.7 and above: declarecontext: React.ContextType<typeofMyContext>
Called when the component may be receiving new props.
React may call this even if props have not changed, so be sure to compare new and existing
props if you only want to handle changes.
Calling Component.setState generally does not trigger this method.
Called when the component may be receiving new props.
React may call this even if props have not changed, so be sure to compare new and existing
props if you only want to handle changes.
Calling Component.setState generally does not trigger this method.
Called immediately before a component is destroyed. Perform any necessary cleanup in this method, such as
cancelled network requests, or cleaning up any DOM elements created in componentDidMount.
getSnapshotBeforeUpdate(prevProps, prevState): any
Runs before React applies the result of Component.render render to the document, and
returns an object to be given to componentDidUpdate. Useful for saving
things such as scroll position before Component.render render causes changes to it.
Note: the presence of this method prevents any of the deprecated
lifecycle events from running.
The most fundamental component for building UI, View is a container that supports layout with flexbox, style, some touch handling, and accessibility controls, and is designed to be nested inside other views and to have 0 to many children of any type. View maps directly to the native view equivalent on whatever platform React is running on, whether that is a UIView,
Hierarchy (view full)
Index
Constructors
Properties
Methods
Constructors
constructor
Parameters
Returns ViewComponent
Parameters
Returns ViewComponent
Deprecated
See
React Docs
Properties
context
If using the new style context, re-declare this in your class to be the
React.ContextType
of yourstatic contextType
. Should be used with type annotation or static contextType.Example
See
React Docs
Readonly
propsrefs
[key: string]: ReactInstance;
}
Type declaration
[key: string]: ReactInstance
Deprecated
See
Legacy React Docs
state
Type declaration
Static
Optional
contextIf set,
this.context
will be set at runtime to the current value of the given Context.Example
See
https://react.dev/reference/react/Component#static-contexttype
Methods
Optional
UNSAFE_Called immediately before mounting occurs, and before Component.render. Avoid introducing any side-effects or subscriptions in this method.
This method will not stop working in React 17.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Returns void
Deprecated
16.3, use componentDidMount or the constructor instead
See
Optional
UNSAFE_Called when the component may be receiving new props. React may call this even if props have not changed, so be sure to compare new and existing props if you only want to handle changes.
Calling Component.setState generally does not trigger this method.
This method will not stop working in React 17.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Parameters
Returns void
Deprecated
16.3, use static getDerivedStateFromProps instead
See
Optional
UNSAFE_Called immediately before rendering when new props or state is received. Not called for the initial render.
Note: You cannot call Component.setState here.
This method will not stop working in React 17.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Parameters
Returns void
Deprecated
16.3, use getSnapshotBeforeUpdate instead
See
Optional
componentCatches exceptions generated in descendant components. Unhandled exceptions will cause the entire component tree to unmount.
Parameters
Returns void
Optional
componentCalled immediately after a component is mounted. Setting state here will trigger re-rendering.
Returns void
Optional
componentCalled immediately after updating occurs. Not called for the initial render.
The snapshot is only present if getSnapshotBeforeUpdate is present and returns non-null.
Parameters
Optional
snapshot: anyReturns void
Optional
componentCalled immediately before mounting occurs, and before Component.render. Avoid introducing any side-effects or subscriptions in this method.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Returns void
Deprecated
16.3, use componentDidMount or the constructor instead; will stop working in React 17
See
Optional
componentCalled when the component may be receiving new props. React may call this even if props have not changed, so be sure to compare new and existing props if you only want to handle changes.
Calling Component.setState generally does not trigger this method.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Parameters
Returns void
Deprecated
16.3, use static getDerivedStateFromProps instead; will stop working in React 17
See
Optional
componentCalled immediately before a component is destroyed. Perform any necessary cleanup in this method, such as cancelled network requests, or cleaning up any DOM elements created in
componentDidMount
.Returns void
Optional
componentCalled immediately before rendering when new props or state is received. Not called for the initial render.
Note: You cannot call Component.setState here.
Note: the presence of getSnapshotBeforeUpdate or getDerivedStateFromProps prevents this from being invoked.
Parameters
Returns void
Deprecated
16.3, use getSnapshotBeforeUpdate instead; will stop working in React 17
See
force Update
Parameters
Optional
callback: (() => void)Returns void
Returns void
Optional
getRuns before React applies the result of Component.render render to the document, and returns an object to be given to componentDidUpdate. Useful for saving things such as scroll position before Component.render render causes changes to it.
Note: the presence of this method prevents any of the deprecated lifecycle events from running.
Parameters
Returns any
render
Returns ReactNode
set State
Type Parameters
Parameters
Optional
callback: (() => void)Returns void
Returns void
Optional
shouldCalled to determine whether the change in props and state should trigger a re-render.
Component
always returns true.PureComponent
implements a shallow comparison on props and state and returns true if any props or states have changed.If false is returned, Component.render,
componentWillUpdate
andcomponentDidUpdate
will not be called.Parameters
Returns boolean
On This Page