ASCII码 ASCII码

通过vue学习react(四) - watch,computed,slot等功能

发布于:2022-03-29 11:47:19  栏目:技术文档

这些功能都比较简单些, 通过react hooks和react class都进行了实现

喜欢就点赞呗

react 实现 watchreact hook 实现 watch

  1. function useWatch<T>(deps: T, handler: (next: T, prev: T) => void, immediate = false) {
  2. let nextRef = useRef<T>();
  3. const isImmediate = useRef(true);
  4. useEffect(() => {
  5. if (isImmediate.current) {
  6. handler(nextRef.current as T, deps);
  7. } else {
  8. isImmediate.current = true;
  9. }
  10. return () => {
  11. nextRef.current = deps;
  12. };
  13. }, [deps]);
  14. }
  15. # 使用
  16. let [count, setCount] = useState(0);
  17. useWatch(count, (next: typeof count, prev: typeof count) => {
  18. console.log(next, prev);
  19. });

react 类写法 实现 watch

  1. class Watch extends React.Component {
  2. state = { count: 0 };
  3. constructor(props: FC) {
  4. super(props);
  5. }
  6. setCount() {
  7. this.setState({
  8. count: this.state.count + 1,
  9. });
  10. }
  11. componentDidUpdate(_prev: any, prevState: typeof this.state) {
  12. console.log(prevState.count, this.state.count);
  13. }
  14. render() {
  15. return (
  16. <div>
  17. <button onClick={this.setCount.bind(this)}>按钮</button>
  18. </div>
  19. );
  20. }
  21. }

react 实现 插槽(slot)react hooks版本

  1. function Child({ children }: { children: React.ReactNode }) {
  2. return (
  3. <div>
  4. child
  5. {children}
  6. </div>
  7. );
  8. }
  9. export default function Slot() {
  10. return (
  11. <div>
  12. <Child>
  13. <p>2333</p>
  14. </Child>
  15. </div>
  16. );
  17. }

react 类写法

  1. class Child extends React.Component {
  2. render() {
  3. const { children } = this.props;
  4. return (
  5. <div>
  6. child
  7. {children}
  8. </div>
  9. );
  10. }
  11. }
  12. class Name extends React.Component {
  13. constructor(props: React.FC) {
  14. super(props);
  15. }
  16. render() {
  17. return (
  18. <Child>
  19. <p>2333</p>
  20. </Child>
  21. );
  22. }
  23. }

react 实现 computedreact hooks版本通过useMemo模拟即可

  1. export default function Computed() {
  2. let [count, setCount] = useState(0);
  3. const getCount = useMemo(() => {
  4. return count * 2;
  5. }, [count]);
  6. return (
  7. <div>
  8. {getCount}
  9. <button
  10. onClick={() => {
  11. setCount(count + 1);
  12. }}>按钮
  13. </button>
  14. </div>
  15. );
  16. }

react 类写法原理则是每次setState都会触发render, 然后重新获取newCount

  1. class Name extends React.Component {
  2. state = { count: 0 };
  3. constructor(props: React.FC) {
  4. super(props);
  5. }
  6. get newCount() {
  7. return this.state.count + 1;
  8. }
  9. render() {
  10. return (
  11. <div>
  12. <p>{this.newCount}</p>
  13. </div>
  14. );
  15. }
  16. }

react 实现 v-modelreact hooks版本

  1. interface formProps {
  2. name?: string;
  3. age?: string;
  4. }
  5. export default function Model() {
  6. const [form, setForm] = useState<formProps>({});
  7. const handleChange = useCallback((e: ChangeEvent<HTMLInputElement>, key) => {
  8. setForm((raw) => ({ ...raw, [key]: e.target.value }));
  9. }, []);
  10. function onClick() {
  11. console.log(form);
  12. }
  13. return (
  14. <div>
  15. <input type="text" value={form.name ?? ""} onChange={(e) => handleChange(e, "name")} />
  16. <input type="text" value={form.age ?? ""} onChange={(e) => handleChange(e, "age")} />
  17. <button onClick={onClick}>按钮</button>
  18. </div>
  19. );
  20. }

react 类写法

  1. interface formProps {
  2. name?: string;
  3. age?: string;
  4. }
  5. class Model extends React.Component<unknown, { form: formProps }> {
  6. constructor(props: React.FC) {
  7. super(props);
  8. this.state = { form: {} };
  9. }
  10. handleChange(e: React.ChangeEvent<HTMLInputElement>, key: string) {
  11. this.setState({
  12. form: { ...this.state.form, [key]: e.target.value },
  13. });
  14. }
  15. onClick = () => {
  16. console.log(this.state.form);
  17. };
  18. render() {
  19. const { form } = this.state;
  20. return (
  21. <div>
  22. <input type="text" value={form.name ?? ""} onChange={(e) => this.handleChange(e, "name")} />
  23. <input type="text" value={form.age ?? ""} onChange={(e) => this.handleChange(e, "age")} />
  24. <button onClick={this.onClick}>按钮</button>
  25. </div>
  26. );
  27. }
  28. }

react 实现 css scopedcss module

  1. # index.module.css
  2. .app {
  3. color: aqua;
  4. flex: 1;
  5. }
  6. # Css.tsx
  7. import Style from "./index.module.css";
  8. export default function Css() {
  9. return (
  10. <div>
  11. <p className={Style.app}>我是一个字段</p>
  12. </div>
  13. );
  14. }

css in js (emotion)安装

  1. npm i @emotion/styled @emotion/react

使用

  1. /* @jsxImportSource @emotion/react */
  2. import styled from "@emotion/styled";
  3. import { css } from "@emotion/react";
  4. import Style from "./index.module.css";
  5. const base = css`
  6. color: #ee298c;
  7. `;
  8. const Container = styled.div`
  9. padding: 23px;
  10. &:hover {
  11. color: ${(props) => props.color};
  12. }
  13. `;
  14. export default function Css() {
  15. return (
  16. <Container color="blue">
  17. <p className={Style.app}>我是一个字段</p>
  18. <p
  19. css={css`
  20. ${base};
  21. background-color: #eee;
  22. `}
  23. >
  24. test
  25. </p>
  26. <p className="test">23333</p>
  27. </Container>
  28. );

以上就是全部内容,希望对大家有所帮助,

相关推荐
阅读 +