Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React "after render" code?

I have an app where I need to set the height of an element (lets say "app-content") dynamically. It takes the height of the "chrome" of the app and subtracts it and then sets the height of the "app-content" to fit 100% within those constraints. This is super simple with vanilla JS, jQuery, or Backbone views, but I'm struggling to figure out what the right process would be for doing this in React?

Below is an example component. I want to be able to set app-content's height to be 100% of the window minus the size of the ActionBar and BalanceBar, but how do I know when everything is rendered and where would I put the calculation stuff in this React Class?

/** @jsx React.DOM */ var List = require('../list'); var ActionBar = require('../action-bar'); var BalanceBar = require('../balance-bar'); var Sidebar = require('../sidebar'); var AppBase = React.createClass({   render: function () {     return (       <div className="wrapper">         <Sidebar />         <div className="inner-wrapper">           <ActionBar title="Title Here" />           <BalanceBar balance={balance} />           <div className="app-content">             <List items={items} />           </div>         </div>       </div>     );   } });  module.exports = AppBase; 
like image 911
Oscar Godson Avatar asked Oct 24 '14 21:10

Oscar Godson


People also ask

Is useEffect called after render?

Does useEffect run after every render? Yes! By default, it runs both after the first render and after every update.

Does useEffect finish before rendering?

Although useEffect is deferred until after the browser has painted, it's guaranteed to fire before any new renders. React will always flush a previous render's effects before starting a new update. This is a good guarantee — you can be sure no updates are missed.

What happens when React re renders?

A second or subsequent render to update the state is called as re-rendering. React components automatically re-render whenever there is a change in their state or props. A simple update of the state, from anywhere in the code, causes all the User Interface (UI) elements to be re-rendered automatically.

When useEffect run?

1. Side Effect Runs After Every Render. The first is the default case. If you do not pass the dependency array to the useEffect hook, the callback function executes on every render.


1 Answers

componentDidMount()

This method is called once after your component is rendered. So your code would look like so.

var AppBase = React.createClass({   componentDidMount: function() {     var $this = $(ReactDOM.findDOMNode(this));     // set el height and width etc.   },    render: function () {     return (       <div className="wrapper">         <Sidebar />           <div className="inner-wrapper">             <ActionBar title="Title Here" />             <BalanceBar balance={balance} />             <div className="app-content">               <List items={items} />           </div>         </div>       </div>     );   } }); 
like image 125
Harborhoffer Avatar answered Oct 11 '22 14:10

Harborhoffer