如何在按下时组合多个状态?

huangapple go评论57阅读模式
英文:

How to combine multiple states with onpress?

问题

这是你要翻译的部分:

"我有一个React Native应用程序。我想要切换两个图标(downcircle和upcircle)。但是onPress触发了一个useState。那么如何组合两个useState?因此,默认状态是图标向上。但是当再次触发onPress时,应该显示图标向下。

我尝试运行以下代码:

const { toggle, showAccordion } = useContext(ToggleContext);
const [toggleIcon, setToggleIcon] = useState(false);
const { accordionItems } = useContext(AccordionItemsContext);

const handleClick = () => {
    setToggleIcon(!toggleIcon);
};

const handleSearch = (query) => {
    setSearchAnimal(query);
};

<View>
    {!item.hasOwnProperty("animals") && (
        <ButtonDetail
            onPress={() => {
                toggle(item.id);
                handleClick();
            }}>
            {toggleIcon ? (
                <AntDesign name="downcircle" size={17} color="green" />
            ) : (
                <AntDesign name="upcircle" size={17} color="green" />
            )}
        </ButtonDetail>
    )}
</View>

ButtonDetail:

import { TouchableOpacity } from "react-native";
import styled from "styled-components/native";

export const ButtonDetail = styled(TouchableOpacity)`
    position: absolute;
    right: 25px;
    width: 25px;
    bottom: 10px;
`;

但是当我像上面那样做时,我收到以下错误:

TypeError: setToggleIcon is not a function. (In 'setToggleIcon(!toggleIcon)', 'setToggleIcon' is undefined)

问题:如何切换两个图标?"

请注意,我已经省略了代码中的HTML实体字符,并将代码片段改为JavaScript的语法以供更好的理解。

英文:

I have a react-native app. And I want to toggle two icons(downcircle and upcircle). But the onpress triggers already a usestate. So how to combine two use states? So the default state is the icon up. But when you trigger the onPress again the icon down has to be shown.

this code I try to run:


	const { toggle, showAccordion } = useContext(ToggleContext);
	const { toggleIcon, setToggleIcon } = useState(false);
	const { accordionItems } = useContext(AccordionItemsContext);

	const handleClick = () =&gt; {
		setToggleIcon(!toggleIcon);
	};

	const handleSearch = (query) =&gt; {
		setSearchAnimal(query);
	};

&lt;View&gt;
								{!item.hasOwnProperty(&quot;animals&quot;) &amp;&amp; (
									&lt;ButtonDetail
										onPress={() =&gt; {
											toggle(item.id);
											handleClick;
										}}&gt;
										{toggleIcon ? (
											&lt;AntDesign name=&quot;downcircle&quot; size={17} color=&quot;green&quot; /&gt;
										) : (
											&lt;AntDesign name=&quot;upcircle&quot; size={17} color=&quot;green&quot; /&gt;
										)}
									&lt;/ButtonDetail&gt;
								)}
							&lt;/View&gt;

ButtonDetail:

import { TouchableOpacity } from &quot;react-native&quot;;
import styled from &quot;styled-components/native&quot;;

export const ButtonDetail = styled(TouchableOpacity)`
	position: absolute;
	right: 25px;
	width: 25px;
	bottom: 10px;
`;

And when I do it like above. I get this error:

TypeError: setToggleIcon is not a function. (In &#39;setToggleIcon(!toggleIcon)&#39;, &#39;setToggleIcon&#39; is undefined)

Question: how to toggle two icons?

答案1

得分: 1

错误信息说明了问题所在 - 传递给 onPress 的内容并非完全符合预期。要解决此错误,您需要通过以下方式重写代码:

//...
&lt;ButtonDetail onPress={() =&gt; {
  toggle(item.id);
  handleClick();
}}&gt;
//...

onPress 不应该(也不能)在其内部接受一个数组,它接受一个函数,您可以在其中描述在单击元素时会发生的所有行为。

英文:

The error text says what the problem is - not exactly what is expected is transmitted to onPress. To fix this error you need to fix the code by rewriting it as follows:

//...
&lt;ButtonDetail onPress={() =&gt; {
  toggle(item.id);
  handleClick();
}}&gt;
//...

onPress should not (and cannot) accept an array inside itself, it accepts a function in which you can describe all the behavior you need that will occur when you click on an element

huangapple
  • 本文由 发表于 2023年7月31日 23:25:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/76805039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定