react - ReactJS Component - react js - reactjs
What is Component API in Reactjs ?
- In React component API has three methods:
- setState(),
- forceUpdate and
- ReactDOM.findDOMNode().
- In new ES6 classes we have to manually bind this.
- You will see in examples below that we are using this.method.bind(this).
- Instead of a React Component are created internally in React when rendering.These instances are reused in subsequent renders,and can be accessed in your component methods as this.
learn reactjs tutorial -
reactjs component API set state
- reactjs example - react tutorial - reactjs - react
Set State
- setState() method is used for updating the state of the component.
- This method will not replace the state but only add changes to original state.
- We started with empty array. Every time we click the button, the state will be updated. If we click it five times, we will get the following output.
Output
learn reactjs tutorial -
reactjs component API set state
- reactjs example - react tutorial - reactjs - react
Force Update
- Sometimes you want to update the component manually.
- You can achieve this by using forceUpdate() method.
- We are setting random number that will be updated every time the button is clicked.
Output
learn reactjs tutorial -
reactjs component API force update
- reactjs example - react tutorial - reactjs - react
Find Dom Node
- For DOM manipulation, we can use ReactDOM.findDOMNode() method.
- First we need to import react-dom.
- The color of myDiv element is changed to green, once the button is clicked.
Output
learn reactjs tutorial -
reactjs component API find dom node
- reactjs example - react tutorial - reactjs - react
Note:Since the 0.14 update, most of the older component API methods are deprecated or removed to accommodate ES6.