Introduction
In the previous article, we have learned about Higher-Order Components in React along with its usage. In this article, we will go through Render Props and the concept of Context in React.
Render Props
Render Props in React is something where a component’s prop is assigned a function and that is called in the render method of the component. Calling the function will return a React element or component. Third-party libraries like React Router and Downshift uses this approach.
A render prop is a function prop that the component uses to know what to render. Through this, we can also pass a state to another.
Let's see a demo.
Create a Movement.js file and code as below,
- import React, { Component } from 'react'
- export class Movement extends Component {
- constructor(props) {
- super(props)
- this.state = {
- x:0,
- y:0
- }
- }
- onMouseMove =(event) => {
- this.setState({
- x:event.clientX,
- y:event.clientY
- })
- }
- render() {
- const{x,y} = this.state
- return (
- <div style={{height : '100%'}} onMouseMove={this.onMouseMove}>
- <h1> The current mouse position is ({x},{y})</h1>
- </div>
- )
- }
- }
- export default Movement
- import React from 'react';
- import './App.css';
- import './css/custom.css';
- import Movement from './components/Movement'
- function App() {
- return (
- <div>
- <Movement></Movement>
- </div>
- );
- }
- export default App;
The above code will display output as below.

Now let’s go further with the demo, we will create a component named Monkey in which on moving the mouse, the monkey will also move following the banana.
Let’s see the demo. Change the code of Movement.js as below.
- import React, { Component } from 'react'
- import PropTypes from 'prop-types'
- export class Movement extends Component {
- static propTypes = {
- render: PropTypes.func.isRequired
- }
- state={x:0,y:0}
- onMouseMove =(event) => {
- this.setState({
- x:event.clientX,
- y:event.clientY
- })
- }
- render() {
- return (
- <div onMouseMove={this.onMouseMove}>
- {this.props.render(this.state)}
- </div>
- )
- }
- }
- export default Movement
- import React, { Component } from 'react'
- import monkey from '../images/monkey.jpg'
- import banana from '../images/banana.jpg'
- export class Monkey extends Component {
- render() {
- const{x,y} = this.props.Movement
- return (
- <div>
- <img src={monkey} alt='monkey' style={{position:'relative',left:x,right:y,height:150}}></img>
- <img src={banana} alt='banana' style={{position:'relative',left:(x+10),right:(y+10),height:30}}></img>
- </div>
- )
- }
- }
- export default Monkey
- import React from 'react';
- import './App.css';
- import './css/custom.css';
- import Movement from './components/Movement'
- import Monkey from './components/Monkey'
- function App() {
- return (
- <div>
- <Movement render={({x,y}) => (
- <Monkey Movement={{x,y}}/>
- )}/>
- </div>
- );
- }
- export default App;
This will display the output as below.

In the above image, on moving the mouse, the monkey will also move. The above demo is quite interesting and lets us allow to animate other animation can be created using react-motion API.
Now, we will see the concept of Context in ReactJS.
Context
Context in React.js is the concept to pass data through a component tree without passing props down manually to each level.
In basic React application, to pass data from parent to child is done using props but this is a heavy approach if data needs to be passed on multiple levels like passing username or theme required by most of the components in the application. Context lets you provide the functionality to share values among components without explicitly passing props through every level of the component tree.
Mainly context is used when the current authentication, themes, or preferred language need to be passed to the child levels when not required to pass props.
To get data using context we need to follow 3 steps,
- We need to first create the context.
- Then we need to provide value to the context.
- Lastly, we need to consume the value of context.
Now let’s have a look at the demo,
For Example - In the current demo, we will see how to use username in child component using context.
First, we will create a new file namely UserContext.js. This is the first step where we are creating context
- import React from 'react'
- const UserContext = React.createContext()
- const UserProvider = UserContext.Provider
- const UserConsumer = UserContext.Consumer
- export {UserProvider,UserConsumer}
Now, we will create a nested structure in which I will create 3 level components Master Component -> Home Component -> Profile Component.
App.js in which we will execute second step we need to provide value to consumer,
- import React from 'react';
- import './App.css';
- import './css/custom.css';
- import MasterComponent from './components/MasterComponent'
- import { UserProvider } from './components/UserContext';
- function App() {
- return (
- <div className="App">
- <UserProvider value="New User">
- <MasterComponent />
- </UserProvider>
- </div>
- );
- }
- export default App;





Mahesh VermaPosted Sep 5, 2019, 1:50 AM
Very informative....thanks for sharing.