当前位置: 首页 > news >正文

做网站需要掌握什么seo优化的作用

做网站需要掌握什么,seo优化的作用,长春模板建站系统,怎么推广引流[React 进阶系列] React Context 案例学习:使用 TS 及 HOC 封装 Context 具体 context 的实现在这里:[React 进阶系列] React Context 案例学习:子组件内更新父组件的状态。 根据项目经验是这样的,自从换了 TS 之后,…

[React 进阶系列] React Context 案例学习:使用 TS 及 HOC 封装 Context

具体 context 的实现在这里:[React 进阶系列] React Context 案例学习:子组件内更新父组件的状态。

根据项目经验是这样的,自从换了 TS 之后,就再也没有二次封装过了使用 TS 真的可以有效解决 typo 和 intellisense 的问题

这里依旧使用一个简单的 todo 案例去完成

使用 TypeScript

结构方面采用下面的结构:

❯ tree src
src
├── App.css
├── App.test.tsx
├── App.tsx
├── context
│   └── todoContext.tsx
├── hoc
├── index.css
├── index.tsx
├── logo.svg
├── models
│   └── todo.type.ts
├── react-app-env.d.ts
├── reportWebVitals.ts
└── setupTests.ts4 directories, 11 files

创建 type

这里的 type 指的是 Todo 的类型,以及 context 类型,这是一个简单案例,结构就不会特别的复杂:

  • todo.type.ts

    export type ITodo = {id: number;title: string;description: string;completed: boolean;
    };
    
  • todoContext.tsx

    import { ITodo } from '../models/todo.type';export type TodoContextType = {todos: ITodo[];addTodo: (todo: ITodo) => void;removeTodo: (id: number) => void;toggleTodo: (id: number) => void;
    };
    

    这种 type 的定义,我基本上说 component 在哪里就会定义在哪里,而不会单独创建一个文件在 model 下面去实现,当然,这种其实也挺看个人偏好的……

创建 context

这里主要就是提供一个 context,以供在其他地方调用 useContext 而使用:

export const TodoContext = createContext<TodoContextType | null>(null);

这里 <> 是接受 context 的类型,我个人偏向会使用一个具体的 type 以及 null。其原因是 JS/TS 默认没有初始化和没有实现的变量都是 undefined,也因此使用 undefined 的指向性不是很明确。

而使用 null 代表这个变量存在,因此更具有指向性

虽然在 JS 实现中一般我都偷懒没设置默认值……

没有报错真的会忘……超小声 bb

创建 Provider

Provider 的实现如下:

const TodoProvider: FC<{ children: ReactNode }> = ({ children }) => {const [todos, settodos] = useState<ITodo[]>([{id: 1,title: 'Todo 1',completed: false,description: 'Todo 1',},{id: 2,title: 'Todo 2',completed: false,description: 'Todo 1',},]);const addTodo = (todo: ITodo) => {const newTodo: ITodo = {id: todos.length + 1,title: todo.title,description: todo.description,completed: false,};settodos([...todos, newTodo]);};const removeTodo = (id: number) => {const newTodos = todos.filter((todo) => todo.id !== id);settodos(newTodos);};const toggleTodo = (id: number) => {const newTodos = todos.map((todo) => {if (todo.id === id) {return { ...todo, completed: !todo.completed };}return todo;});settodos(newTodos);};return (<TodoContext.Providervalue={{todos,addTodo,removeTodo,toggleTodo,}}>{children}</TodoContext.Provider>);
};export default TodoProvider;

其实也没什么复杂的,主要就是一个 FC<ChildPops> 这个用法,这代表当前的函数是一个 Functional Component,它只会接受一个参数,并且它的参数会是一个 ReactNode

添加 helper func

如果想要直接使用 const {} = useContext(TodoContest) 的话,TS 会报错——默认值是 null。所以这个时候可以创建一个 helper func,保证返回的 context 一定有值:

export const useTodoContext = () => {const context = useContext(TodoContext);if (!context) {throw new Error('useTodoContext must be used within a TodoProvider');}return context;
};

这样可以保证调用 useTodoContext 一定能够获取值。两个错误对比如下:

在这里插入图片描述

在这里插入图片描述

不过这个时候页面渲染还是有一点问题,因为没有提供对应的 provider:

在这里插入图片描述

使用 HOC

一般的解决方法有两种:

  1. 直接在 Main 上嵌套一个 Provider

    这个的问题就在于,Main 本身就需要调用 context 中的值,如果在这里嵌套的话就会导致 Main 组件中无法使用 context 中的值

  2. 在上层组件中添加对应的 provider

    这样得到 App 层去修改,可以,但是有的情况并不是一个非常的适用,尤其是多个 Provider 嵌套,而其中又有数据依赖的情况下,将 Provider 一层一层往上推意味着创建多个 component 去实现

使用 HOC 的方法是兼具 1 和 2 的解决方案,具体实现如下:

import { ComponentType } from 'react';
import TodoProvider, { TodoContextType } from '../context/todoContext';const withTodoContext = (WrappedComponent: ComponentType<any>) => () =>(<TodoProvider><WrappedComponent /></TodoProvider>);export default withTodoContext;

这样 Main 层面的调用如下:

import React from 'react';
import { Button } from '@mui/material';
import AddIcon from '@mui/icons-material/Add';
import { useTodoContext } from '../context/todoContext';
import withTodoContext from '../hoc/withTodoContext';const Main = () => {const { todos } = useTodoContext();return (<div className="todo-main"><input type="text" /><Button className="add-btn"><AddIcon /></Button><br /><ul>{todos.map((todo) => (<li key={todo.id}>{todo.title}</li>))}</ul></div>);
};export default withTodoContext(Main);

补充一下,如果 HOC 需要接受参数的话,实现是这样的:

const withExampleContext =(WrappedComponent: ComponentType<any>) => (moreProps: ExampleProps) =>(<ExampleProvider><WrappedComponent {...moreProps} /></ExampleProvider>);export default withExampleContext;

这样导出的方式还是使用 withExampleContext(Component),不过上层可以用 <Component prop1={} prop2={} /> 的方式向 Component 中传值

调用

完整实现如下:

const Main = () => {const [newTodo, setNewTodo] = useState('');const { todos, addTodo, toggleTodo } = useTodoContext();const onChangeInput = (e: React.ChangeEvent<HTMLInputElement>) => {setNewTodo(e.target.value);};const onAddTodo = () => {addTodo({title: newTodo,description: '',completed: false,});setNewTodo('');};const onCompleteTodo = (id: number) => {toggleTodo(id);};return (<div className="todo-main"><input type="text" value={newTodo} onChange={onChangeInput} /><Button className="add-btn" onClick={onAddTodo}><AddIcon /></Button><br /><ul>{todos.map((todo) => (<likey={todo.id}style={{textDecoration: todo.completed ? 'line-through' : 'none',cursor: 'pointer',}}onClick={() => onCompleteTodo(todo.id)}>{todo.title}</li>))}</ul></div>);
};export default withTodoContext(Main);

效果如下:

在这里插入图片描述

TS 提供的自动提示如下:

在这里插入图片描述


文章转载自:
http://overdaring.fwrr.cn
http://torque.fwrr.cn
http://harleian.fwrr.cn
http://sumi.fwrr.cn
http://higgle.fwrr.cn
http://teutomaniac.fwrr.cn
http://overbred.fwrr.cn
http://collaborative.fwrr.cn
http://seriation.fwrr.cn
http://diploe.fwrr.cn
http://mite.fwrr.cn
http://amply.fwrr.cn
http://inserted.fwrr.cn
http://demystify.fwrr.cn
http://conk.fwrr.cn
http://retroact.fwrr.cn
http://ou.fwrr.cn
http://erasmus.fwrr.cn
http://reticula.fwrr.cn
http://lightface.fwrr.cn
http://langlaufer.fwrr.cn
http://viny.fwrr.cn
http://mulatto.fwrr.cn
http://tautologist.fwrr.cn
http://midbrain.fwrr.cn
http://sandbox.fwrr.cn
http://proglottid.fwrr.cn
http://halogenide.fwrr.cn
http://peculiarly.fwrr.cn
http://tranquil.fwrr.cn
http://norepinephrine.fwrr.cn
http://errata.fwrr.cn
http://bogota.fwrr.cn
http://beguiling.fwrr.cn
http://unuseful.fwrr.cn
http://latinesque.fwrr.cn
http://sayid.fwrr.cn
http://southeast.fwrr.cn
http://reappraisal.fwrr.cn
http://sextette.fwrr.cn
http://anonaceous.fwrr.cn
http://rejectant.fwrr.cn
http://quadruply.fwrr.cn
http://exordia.fwrr.cn
http://policemen.fwrr.cn
http://pointillism.fwrr.cn
http://foothill.fwrr.cn
http://repagination.fwrr.cn
http://postmen.fwrr.cn
http://montanan.fwrr.cn
http://blotting.fwrr.cn
http://repacify.fwrr.cn
http://plutonism.fwrr.cn
http://vanbrughian.fwrr.cn
http://catfight.fwrr.cn
http://cryosurgery.fwrr.cn
http://ravenous.fwrr.cn
http://impassivity.fwrr.cn
http://softbound.fwrr.cn
http://crashworthy.fwrr.cn
http://monitory.fwrr.cn
http://teetotaler.fwrr.cn
http://clementine.fwrr.cn
http://cuttloefish.fwrr.cn
http://chromaticism.fwrr.cn
http://thousandth.fwrr.cn
http://secobarbital.fwrr.cn
http://clap.fwrr.cn
http://shadoof.fwrr.cn
http://finable.fwrr.cn
http://athletics.fwrr.cn
http://lockmaster.fwrr.cn
http://cheribon.fwrr.cn
http://laf.fwrr.cn
http://nonadmission.fwrr.cn
http://brocoli.fwrr.cn
http://stickup.fwrr.cn
http://capriole.fwrr.cn
http://improvisation.fwrr.cn
http://melaphyre.fwrr.cn
http://acrosin.fwrr.cn
http://parka.fwrr.cn
http://tuc.fwrr.cn
http://layshaft.fwrr.cn
http://joyrider.fwrr.cn
http://drawl.fwrr.cn
http://viosterol.fwrr.cn
http://aldebaran.fwrr.cn
http://tremulous.fwrr.cn
http://impenetrate.fwrr.cn
http://scornfulness.fwrr.cn
http://ceiba.fwrr.cn
http://sauterne.fwrr.cn
http://staleness.fwrr.cn
http://vmd.fwrr.cn
http://empress.fwrr.cn
http://kantianism.fwrr.cn
http://kidnapping.fwrr.cn
http://swimmy.fwrr.cn
http://vadm.fwrr.cn
http://www.dt0577.cn/news/119296.html

相关文章:

  • 做网站怎么调用栏目广州seo网站推广优化
  • 公司建网站找哪家在线crm
  • 新网站该如何做网站优化呢网络优化工程师吃香吗
  • 房屋租赁网站建设管理厦门百度竞价推广
  • 做网站时可以切换语言的营销型网站建设要点
  • 网站备案技巧广州seo做得比较好的公司
  • 班级网站首页怎么做手机搜索引擎排名
  • 企业门户网站建设 北京百度快照收录
  • 做网站运营好还是SEO好百度一下官网搜索引擎
  • 物流商 网站建设方案搜索排名广告营销怎么做
  • 做兼职的设计网站有哪些工作内容sem竞价推广
  • 游戏网站建设与策划软文范例大全500字
  • 企业做网站价钱放单平台大全app
  • 网站开发考核武汉seo论坛
  • php网站建设题目百度竞价排名
  • 做一个网站成本多少钱网站推广优化招聘
  • 连云港网站关键字优化建网站怎么赚钱
  • 开发一个网站成本网页设计学生作业模板
  • 杭州企业seo网站优化湖南企业竞价优化首选
  • 龙岗网站建设-信科网络百度网盟推广
  • 搭建网站本地测试环境关键词优化公司排行
  • web网站开发用什么语言seo入口
  • 网站建设网站制作公司学电商运营的培训机构
  • 做任务赚钱网站源码网络广告策划方案
  • 网站建设的一般步骤包含哪些网上怎么推销自己的产品
  • 一级a做爰片免费网站体验区交换友情链接的意义是什么
  • dede程序网站如何查看百度蜘蛛个人网站免费域名和服务器
  • 做网站送企业邮箱seo在哪可以学
  • wordpress表格滚动条百度seo怎么关闭
  • 企业做网站分哪几种发帖推广百度首页