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

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

How to combine multiple states with onpress?

问题

这是你要翻译的部分:

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

我尝试运行以下代码:

  1. const { toggle, showAccordion } = useContext(ToggleContext);
  2. const [toggleIcon, setToggleIcon] = useState(false);
  3. const { accordionItems } = useContext(AccordionItemsContext);
  4. const handleClick = () => {
  5. setToggleIcon(!toggleIcon);
  6. };
  7. const handleSearch = (query) => {
  8. setSearchAnimal(query);
  9. };
  10. <View>
  11. {!item.hasOwnProperty("animals") && (
  12. <ButtonDetail
  13. onPress={() => {
  14. toggle(item.id);
  15. handleClick();
  16. }}>
  17. {toggleIcon ? (
  18. <AntDesign name="downcircle" size={17} color="green" />
  19. ) : (
  20. <AntDesign name="upcircle" size={17} color="green" />
  21. )}
  22. </ButtonDetail>
  23. )}
  24. </View>

ButtonDetail:

  1. import { TouchableOpacity } from "react-native";
  2. import styled from "styled-components/native";
  3. export const ButtonDetail = styled(TouchableOpacity)`
  4. position: absolute;
  5. right: 25px;
  6. width: 25px;
  7. bottom: 10px;
  8. `;

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

  1. 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:

  1. const { toggle, showAccordion } = useContext(ToggleContext);
  2. const { toggleIcon, setToggleIcon } = useState(false);
  3. const { accordionItems } = useContext(AccordionItemsContext);
  4. const handleClick = () =&gt; {
  5. setToggleIcon(!toggleIcon);
  6. };
  7. const handleSearch = (query) =&gt; {
  8. setSearchAnimal(query);
  9. };
  10. &lt;View&gt;
  11. {!item.hasOwnProperty(&quot;animals&quot;) &amp;&amp; (
  12. &lt;ButtonDetail
  13. onPress={() =&gt; {
  14. toggle(item.id);
  15. handleClick;
  16. }}&gt;
  17. {toggleIcon ? (
  18. &lt;AntDesign name=&quot;downcircle&quot; size={17} color=&quot;green&quot; /&gt;
  19. ) : (
  20. &lt;AntDesign name=&quot;upcircle&quot; size={17} color=&quot;green&quot; /&gt;
  21. )}
  22. &lt;/ButtonDetail&gt;
  23. )}
  24. &lt;/View&gt;

ButtonDetail:

  1. import { TouchableOpacity } from &quot;react-native&quot;;
  2. import styled from &quot;styled-components/native&quot;;
  3. export const ButtonDetail = styled(TouchableOpacity)`
  4. position: absolute;
  5. right: 25px;
  6. width: 25px;
  7. bottom: 10px;
  8. `;

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

  1. 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 的内容并非完全符合预期。要解决此错误,您需要通过以下方式重写代码:

  1. //...
  2. &lt;ButtonDetail onPress={() =&gt; {
  3. toggle(item.id);
  4. handleClick();
  5. }}&gt;
  6. //...

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:

  1. //...
  2. &lt;ButtonDetail onPress={() =&gt; {
  3. toggle(item.id);
  4. handleClick();
  5. }}&gt;
  6. //...

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:

确定