react ts 组件之间的传值【函数式组件】

1.父组件向子组件传值【Props

./src/components/parent.tsx

import { FC, useState } from "react";
import Child from "./child";
import styles from "./styles.module.css";
const Index: FC = () => {
  const [count, setCount] = useState<number>(0);
	
  const addCount = () => {
    setCount(count + 1);
  };

  return (
    <>
      <h2>parent</h2>
      <h2>parent:{count}</h2>
      <button className={styles.button} onClick={addCount}>
        add count ++
      </button>

      <hr />
      <Child data={{ count, setCount }} />
    </>
  );
};

export default Index;

./src/components/child.tsx

import { FC } from "react";

const Index: FC<{
  data: {
    count: number;
    setCount: React.Dispatch<React.SetStateAction<number>>;
  };
}> = ({ data }) => {

  const { count, setCount } = data;

  const addtarget = () => {
    setCount(count + 1);
  };

  return (
    <>
      <h2>child</h2>
      <h2>parent to child:{count}</h2>

      <button style={buttonchild} onClick={addtarget}>
        child-add ++
      </button>
    </>
  );
};

  const buttonchild = {
    color: "red",
    background: "skyblue",
    padding: "10px 20px",
    borderRadius: "4px",
  };

export default Index;

2.子组件向父组件传值 【callback function】

./src/components/interface.ts

interface propsCount {
  count: number;
  setCount: React.Dispatch<React.SetStateAction<number>>;
}

export type { propsCount };

./src/components/parent.tsx

import { FC, useState } from "react";
import Child from "./child";
import styles from "./styles.module.css";
import { propsCount } from "./interface";

const Index: FC = () => {
  const [pCount, setPcount] = useState<number>(0);

  const handleAddCount = (data: propsCount) => {
    const { count } = data;
    setPcount(count);
  };

  const addCount = () => {
    setPcount(pCount + 1);
  };

  return (
    <>
      <h2>parent</h2>
      <h2>child to parent:{pCount}</h2>
      <button className={styles.button} onClick={addCount}>
        add count ++
      </button>

      <hr />
      <Child onAddCount={handleAddCount} />
    </>
  );
};

export default Index;

./src/components/child.tsx

import { FC, useEffect, useState } from "react";
import { propsCount } from "./interface";

const Index: FC<{
  onAddCount: ({ count, setCount }: propsCount) => void;
}> = ({ onAddCount }) => {
  const [count, setCount] = useState<number>(0);

  const addtarget = () => {
    setCount(count + 1);
    onAddCount({ count, setCount });
  };

  useEffect(() => {
    onAddCount({ count, setCount });
  }, [{ count, setCount }]);

  return (
    <>
      <h2>child:{count}</h2>

      <button style={buttonchild} onClick={addtarget}>
        child-add ++
      </button>
    </>
  );
};

const buttonchild = {
    color: "red",
    background: "skyblue",
    padding: "10px 20px",
    borderRadius: "4px",
  };

export default Index;

3.Context Api

./src/components/interface.ts

interface propsCount {
  count: number;
  setCount: React.Dispatch<React.SetStateAction<number>>;
}

export type { propsCount };

./src/components/parent.tsx

import { FC, createContext, useState } from "react";
import Child from "./child";
import styles from "./styles.module.css";
import { propsCount } from "./interface";

const initialPropsCount = {
  count: 0,
  setCount: () => {},
};

export const MyContext = createContext<propsCount>(initialPropsCount);

const Index: FC = () => {
  const [count, setCount] = useState<number>(0);

  const addCount = () => {
    setCount(count + 1);
  };

  return (
    <>
      <h2>parent:{count}</h2>

      <button className={styles.button} onClick={addCount}>
        add count ++
      </button>

      <hr />
      <MyContext.Provider value={{ count, setCount }}>
        <Child />
      </MyContext.Provider>
    </>
  );
};

export default Index;

./src/components/child.tsx

import { FC, useContext } from "react";
import { MyContext } from "./parent";
const Index: FC = () => {
  const data = useContext(MyContext);
  const { count, setCount } = data;

  const addtarget = () => {
    setCount(count + 1);
  };

  return (
    <>
      <h2>child</h2>
      <h2>parent to child:{count}</h2>
      <button style={buttonchild} onClick={addtarget}>
        child-add ++
      </button>
    </>
  );
};

const buttonchild = {
  color: "red",
  background: "skyblue",
  padding: "10px 20px",
  borderRadius: "4px",
};

export default Index;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值