React Hooks useContext 传参数

1、封装 context.js 文件,如下图:

import { createContext } from "react";

const Context = createContext(null);

export default Context;

 2、父级组件引入 context.js 文件,并用Provider包裹子组件,值通过 value 传递,如下图:

import React, { useState, useRef } from "react";

// context
import Context from "./context";

// components
import NodeWrap from "./nodeWrap";
import UseSelect from "./useSelect";

// css
import "./style.scss";

const WorkFlow = (props) => {
  const useSelectRef = useRef(null);
  const [nodeConfig, updateNodeConfig] = useState(props.data || {});
  const handleSelectRole = (type, data) => {
    useSelectRef.current.show(type, data);
  };
  return (
    <div className="workflow-design">
      <div className="workflow-design-box-scale">
        <Context.Provider value={
  { select: handleSelectRole }}>
          <NodeWrap nodeConfig={nodeConfig} />
        </Context.Provider>
        <div class="end-node">
          <div class="end-node-circle"></div>
          <div class="end-node-text">流程结束</div>
        </div>
      </div>
      <UseSelect ref={useSelectRef} />
    </div>
  );
};

export default WorkFlow;

 3、子组件引入 context.js 文件,并通过 useContext 取到对应的值或方法, 如下图:

import React, { useState, useRef, useEffect, useContext } from "react";
import { Button, Drawer, Input, Layout, Form, Alert } from "antd";
import { UserOutlined, FormOutlined, PlusOutlined } from "@ant-design/icons";

// context
import Context from "../context";

// components
import AddNode from "../addNode";

// css
import "./style.scss";

const { Header, Footer, Content } = Layout;
const Promoter = (props) => {
  const { select } = useContext(Context);
  const nodeTitleRef = useRef(null);
  const [form, updateForm] = useState({});
  const [isEditTitle, updateIsEditTitle] = useState(false);
  const [open, updateOpen] = useState(false);
  const [node, updateNode] = useState(props.nodeConfig || {});
  const toText = () => {
    if (node.nodeRoleList && node.nodeRoleList.length > 0) {
      return node.nodeRoleList.map((item) => item.name).join("、");
    } else {
      return "所有人";
    }
  };
  const handleShow = () => {
    const stateForm = { ...node };
    updateForm(stateForm);
    updateIsEditTitle(false);
    updateOpen(true);
  };
  const handleEditTitle = () => {
    updateIsEditTitle(true);
    setTimeout(() => {
      nodeTitleRef.current.focus();
    }, 100);
  };
  const handleSaveTitle = (e) => {
    const stateForm = { ...form };
    stateForm.nodeName = e.target.value;
    updateForm(stateForm);
    updateIsEditTitle(false);
  };
  const handleSelectHandle = (type, list) => {
    select(type, list);
  };
  useEffect(() => {
    updateNode(props.nodeConfig);
  }, []);
  return (
    <React.Fragment>
      <div className="node-wrap">
        <div className="node-wrap-box start-node" onClick={handleShow}>
          <div className="title" style={
  { background: "#576a95" }}>
            <UserOutlined />
            <span>{node.nodeName}</span>
          </div>
          <div className="content">
            <span>{toText()}</span>
          </div>
        </div>
        <AddNode nodes={node.childNode} />
      </div>
      <Drawer
        className="promoter-drawer"
        placement="right"
        open={open}
        closable={false}
        width={500}
      >
        <div className="node-wrap-drawer-title">
          {!isEditTitle ? (
            <label onClick={handleEditTitle}>
              <span>{form.nodeName}</span>
              <FormOutlined className="node-wrap-drawer-title-edit" />
            </label>
          ) : (
            <Input
              ref={nodeTitleRef}
              defaultValue={form.nodeName}
              onBlur={(e) => handleSaveTitle(e)}
            />
          )}
        </div>
        <Content>
          <Form label-position="top">
            <Form.Item label="谁可以发起此审批">
              <Button
                type="primary"
                shape="round"
                size="small"
                icon={<PlusOutlined />}
                onClick={() => handleSelectHandle(2, form.nodeRoleList)}
              >
                选择角色
              </Button>
            </Form.Item>
            {form.nodeRoleList && form.nodeRoleList.length == 0 ? (
              <Alert
                message="不指定则默认所有人都可发起此审批"
                type="warning"
              />
            ) : null}
          </Form>
        </Content>
        <Footer>
          <Button type="primary">保存</Button>
          <Button>取消</Button>
        </Footer>
      </Drawer>
    </React.Fragment>
  );
};

export default Promoter;

个人小结,不喜勿喷。

相关推荐

  1. c++的参数引用参数

    2024-01-12 10:12:02       34 阅读
  2. React Hooks useContext 参数

    2024-01-12 10:12:02       60 阅读
  3. axios query数组参数的格式

    2024-01-12 10:12:02       50 阅读
  4. C# ref参与out

    2024-01-12 10:12:02       48 阅读
  5. 使用OkHttp上本地图片及参数

    2024-01-12 10:12:02       58 阅读
  6. el-upload添加FormData参数,自定义上接口

    2024-01-12 10:12:02       52 阅读
  7. 58.Gin参数读取、绑定、文件上

    2024-01-12 10:12:02       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 10:12:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 10:12:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 10:12:02       87 阅读
  4. Python语言-面向对象

    2024-01-12 10:12:02       96 阅读

热门阅读

  1. py11-python之正则-re

    2024-01-12 10:12:02       41 阅读
  2. jenkins error No space left on device

    2024-01-12 10:12:02       55 阅读
  3. ebpf学习_incomplete

    2024-01-12 10:12:02       44 阅读
  4. MongoDB聚合:$bucketAuto

    2024-01-12 10:12:02       49 阅读
  5. Python(35):Python3 通过https上传文件和下载文件

    2024-01-12 10:12:02       55 阅读
  6. 极狐 GitLab 冷知识:使用 git push 创建 Merge Request

    2024-01-12 10:12:02       55 阅读
  7. 阿里云事件上报-确保发送成功

    2024-01-12 10:12:02       57 阅读
  8. 2024成都无人机展览会/2024成都无人机展览会

    2024-01-12 10:12:02       51 阅读