Giter VIP home page Giter VIP logo

react-demo1's Introduction

一、React-demo1

测试Webpack创建react项目

基于webpack+react+ts实现一个自动匹配搜索框

效果如下:

AutoComplete.gif

使用:

  • 克隆到本地 git clone https://github.com/xllpiupiu/React-demo1.git到本地;
  • 安装相应的包 npm install
  • 本地运行npm run dev
  • 浏览器打开:http://localhost:8080/
  • 测试demo:

输入:

  1. 前端
  2. 后端
  3. 工资
  4. 学历

实现过程

React-demo1

博客地址:React-demo1(自动匹配搜索框) - 掘金 (juejin.cn)

使用webpack构建一个react项目。 参考地址:

https://www.jb51.net/article/247286.htm https://blog.csdn.net/mChales_Liu/article/details/111318858

TypeScript+Webpack+React

上述只是使用webpack构建了简单的react应用,使用的js语法,接下来探索如何用ts编写代码实现对应功能。参考地址:https://zhuanlan.zhihu.com/p/455297005

  • 首先安装与ts相关的插件
npm install awesome-typescript-loader source-map-loader
npm install ts-loader -D 
npm install typescript -D 
npx tsc --init  

上述生成tsconfig.json文件的命令使用npx tsc --init,而不是tsc --init因为,安装的typescript不是全局安装。参考https://blog.csdn.net/qq_41499782/article/details/112368529

在次tsconfig.json文件中:

image.png

解决:修改webpack.common.js中的入口文件,

image.png

  • 配置babel支持typescript,在webpack.common.js修改babel配置,options增加@babel/preset-typescript

image.png

  • 修改tsconfig.jsonjsx项:
    "jsx": "react", 
  • npm run dev之后报错:

image.png

!!!!!忘记安装这个包了,npm install --save-dev @babel/preset-typescript

reactreact-dom这两个包代码中都不存在对应的类型声明,需要单独安装他们对应的类型声明文件: @types/react-dom @types/react

实现自动匹配搜索框

1. 需求分析

AutoComplete.gif

参考百度的搜索框,输入关键字,展示含有关键字的匹配下拉列表。

  • 输入框组件、下拉列表组件;
  • 输入字符串,进行匹配展示含有该字符串相关的数据;
  • 搜索匹配功能流程图: image.png

2.在搜索匹配的时候遇到的问题:

handleSearch(searchVal: string) {
        console.log('AutoComponent----', searchVal);
        console.log('AutoComponent----', data);
        //使用过滤器过滤
        this.setState({
            searchRe: ['dd', '33']
        })
        console.log('过滤', data.filter((item) => item.indexOf(searchVal) !== -1))
        console.log('searchRe----', this.state.searchRe)
        if (this.state.searchRe.length !== 0) {
            //匹配到搜索结果
            console.log('AutoComponent----', this.state.searchRe)//结果如下图所示:
        }
    }

this.state.searchRe结果: image.png 查阅官方文档:state的更新可能是异步的。

image.png

使用到属性dangerouslySetInnerHTML={{__html: item}}

matchRes.map((item: string) => {
            const newItem = item.replace(searchVal, `<span style='color: #9f95f7;'>${searchVal}</span>`);
            newRes.push(newItem);
        });
<ul className="match-ul">
                    {
                        newRes.map((item: string, index: number) => 
                            <li dangerouslySetInnerHTML={{__html: item}} className="item-li" key={index.toString()}>
                            </li>
                        )
                    }
</ul>

3. 结果

AutoComplete.gif

react-demo1's People

Contributors

924160052 avatar xllpiupiu avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.