IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> React 小案例 -> 正文阅读

[JavaScript知识库]React 小案例

React 中使用 css

方式一,内联样式

render() {
    return (
      <>
        <div style={{ width: '100px', height: '100px', backgroundColor: 'pink'}}>react ts</div> 
      </>
    )
  }

方式二,外部样式

/* home.css */
.square{
    width:100px;
    height:100px;
    background-color:pink;
}

.tsx 文件

import React, { Component } from 'react';
import './home.css';
...
render() {
    return (
      <>
        <div className="square">react ts</div> 
      </>
    )
  }

需求:点击按钮更改样式

import React, { Component } from 'react';
import './home.css';

interface IState {
  width: number
}
class App extends Component<any, IState> {
  constructor(props: any) {
    super(props)
    this.state = {
      width: 200
    }
  }
  change = () => {
    this.setState((state) => ({
      // width: 100 也一样
      width: state.width - 100
    }))
  }
  render() {
    return (
      <>
        <div style={{ width: `${this.state.width}px`, height: '100px', backgroundColor: 'pink' }}>react ts</div>
        <br />
        <button onClick={this.change}>改变宽度</button>
      </>
    )
  }
}
export default App;

需求:当 div 的宽度小于 180px 就由正方形变圆形

import React, { Component } from 'react';
import './home.css';

interface IState {
  width: number
}
class App extends Component<any, IState> {
  constructor(props: any) {
    super(props)
    this.state = {
      width: 200
    }
  }
  change = () => {
    this.setState((state) => ({
      // width: 100
      width: state.width - 10
    }))
  }
  render() {
    return (
      <>
        <div className={this.state.width > 180 ? 'square' : 'circle'}></div>
        <br />
        <button onClick={this.change}>减少宽度</button>
      </>
    )
  }
}
export default App;
/* home.css */
.square{
    width:100px;
    height:100px;
    background-color:pink;
}
.circle{
    width: 100px;
    height: 100px;
    background-color:pink;
    border-radius: 50%;
}

封装列表删除按钮组件

子组件

// DeleteUser.tsx
import { Component } from 'react'
import { Button } from 'antd'

interface IProps { // 规定父组件要传的参数接口
    id: number
    callback: (id: number) => void
}

export default class DeleteUser extends Component<IProps, any> {
    DeleteUser = () => {
        this.props.callback(this.props.id)
    }

    render() {
        return (
            <>
                <Button type="primary" danger onClick={this.DeleteUser}>删除</Button>
            </>
        )
    }
}

父组件

import { Component } from 'react';
import DeleteUser from './DeleteUser'
import './home.css';
import 'antd/dist/antd.css'
import { Button, Space, Table } from 'antd';
interface IUser {
  id: number,
  name: string
}
interface IState {
  userList: IUser[]
}
class App extends Component<any, IState> {
  constructor(props: any) {
    super(props)

    let tableList: IUser[] = []

    for (let i = 0; i < 20; i++) { 
      tableList.push({
        id: i,
        name: 'user' + i
      })
    }
    
    this.state = {
      userList: tableList,
    }
  }

  DeleteUser = (id: number) => {
    console.log(id)
    this.setState(() => ({
      userList: this.state.userList.filter(item => item.id !== id)
    }))
  }

  render() {
    return (
      <>
        <Table
          dataSource={this.state.userList}
          rowKey='id'
        >
          <Table.Column title={"ID"} dataIndex='id' />
          <Table.Column title={"姓名"} dataIndex='name' />
          <Table.Column title={"操作"} render={(user: IUser) => (
            <Space>
              <Button type="primary" >编辑</Button>
              <DeleteUser id={user.id} callback={this.DeleteUser} />
            </Space>
          )} />
        </Table>
      </>
    )
  }
}
export default App;
  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2021-11-22 12:15:09  更:2021-11-22 12:17:36 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/20 22:53:25-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码