javascript 掌握反应 - 组件(代码与莫什)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 掌握反应 - 组件(代码与莫什)相关的知识,希望对你有一定的参考价值。

import React, { Component } from "react";

class Counter extends Component {
  render() {
    return (
      <React.Fragment> //Will not need to have a div within the 'root' div
        <h1>Hello World</h1>
        <button>Increment</button>
      </React.Fragment>
    );
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0,
  };

  render() {
    return (
      <div>
    	<span>{this.formatCount()}</span>
    	<button>Increment</button>
      </div>
    );
  }
  
  formatCount(){
  	const {count} = this.state; //Object destructuring
  	return count === 0 > <h1>Zero</h1> : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0,
    imageUrl: 'https://picsum.photos/200'
  };

  styles={
  	fontSize: 50,
  	fontWeight: 'bold'
  };

  render() {
    return (
      <div>
    	<img src={this.state.imageUrl} alt='' />
    	
        <span style={this.styles} className='badge badge-primary m-2'>
        	{this.formatCount()}
        </span>
        <span style={ {fontSize:30}} className='badge badge-primary m-2'>
        	{this.formatCount()}
        </span> //Inline Style
        
        <button className='btn btn-secondary btn-sm'>Increment</button>
      </div>
    );
  }
  
  formatCount(){
  	const {count} = this.state;
  	return count === 0 > <h1>Zero</h1> : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0,
  };

  render() {
    return (
      <div>
        <span className={this.getBadgeClasses()}>
        	{this.formatCount()}
        </span>
      
        <button className='btn btn-secondary btn-sm'>Increment</button>
      </div>
    );
  }
  
  getBadgeClasses(){
  	let classes='badge m-2 badge-'
  	classes += this.state.count === 0 ? 'warning' : 'primary'
  	return classes
  }
  
  formatCount(){
  	const {count} = this.state;
  	return count === 0 > <h1>Zero</h1> : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0,
    tags: ["tag1", "tag2", "tag3"]
  };

  render() {
    return (
      <div>
        <span className={this.getBadgeClasses()}>
        	{this.formatCount()}
        </span>
      
        <button className='btn btn-secondary btn-sm'>Increment</button>
        
        <ul>{this.state.tags.map(tag => <li key={tag}> {tag} </li>}</ul>
      </div>
    );
  }
  
  getBadgeClasses(){
  	let classes='badge m-2 badge-'
  	classes += this.state.count === 0 ? 'warning' : 'primary'
  	return classes
  }
  
  formatCount(){
  	const {count} = this.state;
  	return count === 0 ? <h1>Zero</h1> : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0,
    tags: ["tag1", "tag2", "tag3"]
  };

  renderTags() {
    if (this.state.tags.length === 0) return <p>There are no tags!</p>;

    return (
      <ul>
        {this.state.tags.map(tag => (
          <li key={tag}>{tag}</li>
        ))}
      </ul>
    );
  }

  render() {
    return (
      <div>
        {this.state.tags.length === 0 && "Please Create a new tag!"}
        {this.renderTags()}
      </div>
    );
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0
  };

  // constructor() {
  //   super();
  //   this.handleIncrement = this.handleIncrement.bind(this);
  // }

  handleIncrement = () => {
    console.log("Increment Clicked", this);
  };

  render() {
    return (
      <div>
        <span className={this.getBadgeClasses()}>{this.formatCount()}</span>

        <button
          onClick={this.handleIncrement}
          className="btn btn-secondary btn-sm"
        >
          Increment
        </button>
      </div>
    );
  }

  getBadgeClasses() {
    let classes = "badge m-2 badge-";
    classes += this.state.count === 0 ? "warning" : "primary";
    return classes;
  }

  formatCount() {
    const { count } = this.state;
    return count === 0 ? <h1>Zero</h1> : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0
  };

  // constructor() {
  //   super();
  //   this.handleIncrement = this.handleIncrement.bind(this);
  // }

  handleIncrement = () => {
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <span className={this.getBadgeClasses()}>{this.formatCount()}</span>

        <button
          onClick={this.handleIncrement}
          className="btn btn-secondary btn-sm"
        >
          Increment
        </button>
      </div>
    );
  }

  getBadgeClasses() {
    let classes = "badge m-2 badge-";
    classes += this.state.count === 0 ? "warning" : "primary";
    return classes;
  }

  formatCount() {
    const { count } = this.state;
    return count === 0 ? "Zero" : count;
  }
}

export default Counter;
import React, { Component } from "react";

class Counter extends Component {
  state = {
    count: 0
  };

  // constructor() {
  //   super();
  //   this.handleIncrement = this.handleIncrement.bind(this);
  // }

  handleIncrement = product => {
    console.log(product);
    this.setState({ count: this.state.count + 1 });
  };

  render() {
    return (
      <div>
        <span className={this.getBadgeClasses()}>{this.formatCount()}</span>

        <button
          onClick={() => this.handleIncrement({ id: 1 })} //Use the map method  to pass a list
          className="btn btn-secondary btn-sm"
        >
          Increment
        </button>
      </div>
    );
  }

  getBadgeClasses() {
    let classes = "badge m-2 badge-";
    classes += this.state.count === 0 ? "warning" : "primary";
    return classes;
  }

  formatCount() {
    const { count } = this.state;
    return count === 0 ? "Zero" : count;
  }
}

export default Counter;

以上是关于javascript 掌握反应 - 组件(代码与莫什)的主要内容,如果未能解决你的问题,请参考以下文章

无法在javascript反应中更改警报消息的颜色

javascript 中 - 反应“子组件” - 文章子组件已满

如何在反应组件中使用 javascript 访问 Sass 变量?

javascript 反应组件创建

javascript 反应导航:功能组件标题设置

javascript 反应样本组件