In this tutorial, we are going to discusshow to perform console.log() in react and react native applicationinside the render block. Most of the times developer trying to addconsole.logstatement inside the return block of render block, in that case it will not print the statement inside the console rather it will print the results inside the browser screen.
Lets see the below example, where we are usingconsole.log statement inside the return blockof render function.
Wrong Method
render() { return ( <div> <h1>List of countries</h1> console.log(this.props.countries) </div> ); }
This will not print the expected list in the console. It will just render the stringconsole.log(this.props.countries)in the browser.
Lets see the solution for this example :
Wright Method
Embed the expression in your JSX.
render() { return ( <div> <h1>List of countries</h1> { console.log(this.props.countries) } </div> ); }
Another popular solution widely in use :
Place yourconsole.logbefore thereturn().
render() { console.log(this.props.countries) return ( <div> <h1>List of countries</h1> </div> ); }
Also there is another method for this type of scenario, for that youneed to create custom componentin order to use print console.log statement in react and react native application.
Create custom component for console.log.
const ConsoleLog = ({ children }) => { console.log(children); return false; };
Then you can use it like this :
render() { return ( <div> <h1>List of countries</h1> <ConsoleLog>{ this.props.countries }</ConsoleLog> </div> ); }
This is all aboutconsole.log in react and react native using JSX.
Similar Posts:
- How to deal with the error “cannot read property ‘set state’ of undefined” in react
- Uncaught Error: _registerComponent(…): Target container is not a DOM element
- How to Solve Error: Cannot read property ‘map’ of undefined
- The execution order of return statement in try except else finally in Python
- How to Solve React-native-gesture-handler Error
- Three common ways of react event binding and solution can’t update during an existing state transition (such as within ‘render’)
- DOM style setting methods in the four components of react
- Using react native elements in RN project to report an error: unrecognized font family ‘material icons’
- Uncaught type error: create-react-app and electronfs.existsSync is not a function
- [Solved] React Project Start Error: Uncaught TypeError: Cannot read property ‘forEach‘ of undefined