score:11
the basic difference is, when it a component
, react will run/add all its lifecycle methods. this will be useful when you have state
in your component. when you use this component, react will create a react component instance
which will have all the lifecycle methods and other hooks added to it.
class app extends react.component{
...
}
in some cases, you won't use state
. in those cases, adding all those lifecycle methods are unnecessary. so, react gives you an way to create an component which will have render
alone. it is called purecomponent
. when you use this, there is no need to create a new component instance
because there is no lifecycle methods here. it'll just be a function which can take props
and return react elements.
class app extends react.purecomponent{
...
}
hope this helps!
[update]
what is a component
and a component instance
?
technically, a component
in react is a class
or a function
.
example:
class app extends react.component{
...
}
//stateless component
const app = (props) => {
...
}
when you use that component
, it'll be instantiated, more like new app()
. but, react does it by itself in a different way.
for example:
render(){
return <app/> //instance of the component app
}
instances are required because, each instance can perform individually. instances are a copy of original class.
simple answer is, components
will be a class
and component instance
will be the copy/instance of the class and will be used in render
hope this explains!
score:5
a "react component instance" is just an instance that was created from a previously defined class component. see the example below (es6/jsx) which contains both props and state:
class mycomponentclass extends react.component {
constructor(props) {
super(props);
// set initial state
this.state = {
example: 'example'
};
}
render() {
return <div>
<div>{this.state.example}</div>
<div>{this.props.example}</div>
</div>;
}
}
if you have no need for state in your component you can use a pure, stateless, functional react component like so:
function mystatelessfunctionalcomponent(props) {
return <div>{this.props.example}</div>;
}
here is some more information about stateless react components when they were introduced in react v0.14
. since then you have the ability to use hooks starting in react v16.8
, which allow you to define a functional component that has state or makes use of the component lifecyle.
as mentioned in some other comments, there are many performance benefits when using stateless components. these types of components are perfect for when you want something purely presentational as an example.
since there’s no state or lifecycle methods to worry about, the react team plans to avoid unnecessary checks and memory allocations in future releases.
Source: stackoverflow.com
Related Query
- What is the difference between React component instance property and state property?
- What is the difference between React component and React component instance?
- What is the difference between accessible, accessibilityLabel and accessibilityHint properties of Text component in react native?
- what is the difference between element attribute and component attribute in React <Route><Route/> tag
- In React, what is the difference between using functional component and just a function, which returns React node?
- What is the difference between React functional component <Message id> and js function formatMessage() and which one is better to use?
- What is the difference between React Native and React?
- 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'?
- ReactJS: what is the difference between functional component and class component
- 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
- what is the difference between getDefaultProps and getInitialState react js
- 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 owner and parent component in React.js
- 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's the difference between functional component and create component using useMemo() in React
More Query from same tag
- Insert image within imported React component (SOLVED!)
- wrap a response data with object in javascript
- Sharing state from parent to child component in React
- What are the right types for a useContext with TypeScript/ReactJS?
- Why are my dimensions not updating on resize when using React Hooks
- Install React Unsupported Engine, React
- react state not being updated
- Staying DRY with Async Calls in React Redux Application With Redux Thunk
- ReactJS and Konva free drawing with pencil, brush and eraser
- How to locate react-leaflet map to user's current position and get the borders for this map?
- React Router sending a state
- Angular 2 V/S Angular js V/S React js v/s Typescript
- SVG icon doesn`t load
- React useForm equivalent in class component
- Is it possible to disinguish between server and client in babelrc config (meteor)?
- Nothing is showing when I loop through the array using forEach in react
- react-render-html: TypeError: Cannot read property 'length' of undefined
- Next.js multi level routing
- react-fontawesome not displaying icons
- React Contenteditable Not Focusing with Ref
- ES6 and webpack: Redux: How to import a file from a distant directory
- useFetch custom hooks is somehow returning data twice how to fix that?
- Filter API Object Array by Matching Object Properties
- PropTypes: array of one of the class instance
- Display/Map Nested JSON fields in React JavaScript
- React-tools: app.jsx changed; rebuilding... never ends
- when I click on the icon, It was not redirected to the link. How can I do that?
- React child component do not re-render on it state change
- Error rewriting as a React Functional Component in Typescript: Property 'forceUpdateHandler' does not exist on type 'MutableRefObject<Spinner | null>'
- Resolving Elements in Array when Array Length is 0 (MongoDB, React, Axios)