Home  >  Article  >  Web Front-end  >  What are the different rendering methods of react?

What are the different rendering methods of react?

青灯夜游
青灯夜游Original
2022-03-22 14:06:312808browse

React rendering methods include: 1. Rendering using conditional expressions, suitable for rendering of two components; 2. Rendering using the "&&" operator, suitable for rendering with or without a component; 3. , Use variable output components for rendering; 4. Use functional methods to output components or use functional components for rendering.

What are the different rendering methods of react?

The operating environment of this tutorial: Windows7 system, react17.0.1 version, Dell G3 computer.

Several ways to conditionally render React components

1. Conditional expression rendering (applicable to two components Rendering of one of the two)

render() {
  const isLoggedIn = this.state.isLoggedIn;
  return (
    
{isLoggedIn ? ( ) : ( )}
); }

2. && operator rendering (applicable to rendering with or without a component)

function Mailbox(props) {
  const unreadMessages = props.unreadMessages;
  return (
    

Hello!

{unreadMessages.length > 0 &&

You have {unreadMessages.length} unread messages.

}
); }

3. Use variables to output component rendering (suitable for rendering under multiple components and under various conditions)

render() {
    const isLoggedIn = this.state.isLoggedIn;
 
    const button = isLoggedIn ? (
      
    ) : (
      
    );
 
    return (
      
{button}
); }

4. Use functional methods to output components or use functional components for rendering ( Suitable for situations where multiple sub-components need to be output based on complex conditions)

1. Functional approach

renderButton(){
    const isLoggedIn = this.state.isLoggedIn;
    if(isLoggedIn)
    {
       return ();
    }
    else
    {
      return ();
    }
}
 
render() {
    return (
      
{this.renderButton()}
); }

2. Functional component

function Greeting(props) {
  const isLoggedIn = props.isLoggedIn;
  if (isLoggedIn) {
    return ;
  }
  return ;
}
 
ReactDOM.render(
  // Try changing to isLoggedIn={true}:
  ,
  document.getElementById('root')
);

[Related recommendations: Redis video tutorial

The above is the detailed content of What are the different rendering methods of react?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What is fiber in reactNext article:What is fiber in react