在自定义反应组件中添加一个on Click事件

发布于 2025-01-24 18:40:35 字数 2498 浏览 3 评论 0 原文

我有一个react组件验证editationeditor 调用另一个自定义react component 属性,该保存属性 value a >字符串数组

interface props extends ValidationDataProperty {
  tree: Tree;
}

const ValidationEditor = ({ tree, id, ifData, thenData, elseData }: props) => {
  const { classes } = useStyles();
  const { state, dispatch } = useContext(PropertyContext);
  const updateValidation = modifyConditionalProperty(id, dispatch);
  return (
    <Group className={classes.validator}>
      <Box></Box>
      <Text>If</Text>
      <Property
        value={[""]}
        mode="edit"
        tree={tree}
        onClick={(e: { target: { value: SetStateAction<string[]> } }) =>
          updateValidation({ ifData: { ...ifData, value: e.target.value } })
        }
      />
      <NativeSelect
        data={_.keys(ComparisonType)}
        required
        value={ifData.comparison}
        onChange={(e: { target: { value: SetStateAction<string> } }) =>
          updateValidation({
            ifData: { ...ifData, comparison: e.target.value },
          })
        }
      />{" "}
      <TextInput
        placeholder="Provide conditional value"
        required
        value={ifData.value}
        sx={{ flex: 1 }}
        onChange={(e: { target: { value: SetStateAction<string> } }) =>
          updateValidation({ ifData: { ...ifData, value: e.target.value } })
        }
      />
      
    </Group>
  );
};

export default ValidationEditor;

现在,我想将 onclick 事件添加到属性 React组件。基本上 onclick 我想调用 action - &gt; modifyConditionalProperty(id,dispatch),将更新商店通过还原> 。我只是在努力使其仅适用于我的自定义 react component 属性静止的工作正常。

这就是属性 组件看起来像

interface PropertyProps {
  value?: string[];
  mode: "edit" | "display";
  tree: Tree;
  onClick?: (e: { target: { value: SetStateAction<string[]> } }) => void;
}

const Property = ({ value, mode, tree }: PropertyProps) => {
  const [currentValue, setCurrentValue] = useState<string[]>(value || []);
  const [displayMode, toggle] = useToggle(mode, ["edit", "display"]);
  console.log(value) // ["a", "b", "c"]
  
  return (
    <Box>
      .....
    </Box>
  );
};

export default Property;

I have a React Component ValidationEditor that calls another custom React Component Property, which holds the property value a array of strings.

interface props extends ValidationDataProperty {
  tree: Tree;
}

const ValidationEditor = ({ tree, id, ifData, thenData, elseData }: props) => {
  const { classes } = useStyles();
  const { state, dispatch } = useContext(PropertyContext);
  const updateValidation = modifyConditionalProperty(id, dispatch);
  return (
    <Group className={classes.validator}>
      <Box></Box>
      <Text>If</Text>
      <Property
        value={[""]}
        mode="edit"
        tree={tree}
        onClick={(e: { target: { value: SetStateAction<string[]> } }) =>
          updateValidation({ ifData: { ...ifData, value: e.target.value } })
        }
      />
      <NativeSelect
        data={_.keys(ComparisonType)}
        required
        value={ifData.comparison}
        onChange={(e: { target: { value: SetStateAction<string> } }) =>
          updateValidation({
            ifData: { ...ifData, comparison: e.target.value },
          })
        }
      />{" "}
      <TextInput
        placeholder="Provide conditional value"
        required
        value={ifData.value}
        sx={{ flex: 1 }}
        onChange={(e: { target: { value: SetStateAction<string> } }) =>
          updateValidation({ ifData: { ...ifData, value: e.target.value } })
        }
      />
      
    </Group>
  );
};

export default ValidationEditor;

Now I want to add a onClick event to the Property React Component . Basically onClick I want to call an action -> modifyConditionalProperty(id, dispatch) , that will update the store via a reducer. I am only struggling to make it work only for my custom React component Property rest its working fine.

This is how the Property component looks like

interface PropertyProps {
  value?: string[];
  mode: "edit" | "display";
  tree: Tree;
  onClick?: (e: { target: { value: SetStateAction<string[]> } }) => void;
}

const Property = ({ value, mode, tree }: PropertyProps) => {
  const [currentValue, setCurrentValue] = useState<string[]>(value || []);
  const [displayMode, toggle] = useToggle(mode, ["edit", "display"]);
  console.log(value) // ["a", "b", "c"]
  
  return (
    <Box>
      .....
    </Box>
  );
};

export default Property;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文