为什么我要点击两次才能打开 <Modal /> 当我从一个页面返回时?

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

Why do I have to click two times to open the <Modal /> when I return from a page?

问题

我创建了一个按钮类型,用于在主页上打开模态框,模态框在单击叠加层时可以正常打开和关闭,问题是当我在包含列表的页面使用&lt;Modal /&gt;组件,并且在返回主页后,如果我单击该按钮以打开<Modal />,则第一次单击时它不会打开。我必须点击两次才能打开它。这里问题是什么?我无法理解。

这是我的代码:

英文:

I made a button type to open the modal on the Home page, the modal is coming fine and closing fine when click on the overlay, the problem is when I got some page with the list inside&lt;Modal /&gt; component and when I come back to the Home page and then if I click on that button to open the <Modal /> it doesn't open on first click yet. I have to click two times to open it. What is the issue here? I can't understand.

Here is my code:

//Home.js
const modalData = [
  { key: &quot;Add Dealer&quot;, navigateTo: &quot;AddDealer&quot; },
  { key: &quot;Add Franchise&quot;, navigateTo: &quot;AddFranchise&quot; },
  { key: &quot;New Lead&quot;, navigateTo: &quot;AddNewLeads&quot; },
];

export default function Home() {
  const [modalVisible, setModalVisible] = useState(false);

  const renderPopList = ({ item, index }) =&gt; {
    const lastItem = index === modalData.length - 1;
    return (
      &lt;TouchableOpacity onPress={() =&gt; navigation.navigate(item.navigateTo)}&gt;
        &lt;Text style={[styles.links, lastItem &amp;&amp; styles.lastLink]}&gt;
          {item.key}
        &lt;/Text&gt;
      &lt;/TouchableOpacity&gt;
    );
  };

  const toggleModal = () =&gt; {
    setModalVisible(!modalVisible);
  };
  return (
    &lt;&gt;
      &lt;Modal
        animationType=&quot;slide&quot;
        transparent={true}
        visible={modalVisible}
        onRequestClose={toggleModal}
        onTouchStart={toggleModal}
        style={styles.modalWrapper}
      &gt;
        &lt;TouchableWithoutFeedback onPress={toggleModal}&gt;
          &lt;View
            style={{ flex: 1, backgroundColor: &quot;rgba(0,0,0,0.1)&quot; }}
            onPress={toggleModal}
          &gt;&lt;/View&gt;
        &lt;/TouchableWithoutFeedback&gt;
        &lt;FlatList
          data={modalData}
          renderItem={renderPopList}
          onPress={toggleModal}
          style={[styles.modal]}
        /&gt;
      &lt;/Modal&gt;
      //Button Here
      &lt;View style={styles.bottomBtnWrap}&gt;
        &lt;TouchableOpacity style={styles.addBtn} onPress={toggleModal}&gt;
          &lt;FontAwesome color=&quot;#fff&quot; name=&quot;plus&quot; size={30} /&gt;
        &lt;/TouchableOpacity&gt;
      &lt;/View&gt;
    &lt;/&gt;
  );
}

答案1

得分: 0

你可以使用这段代码进行操作:

//Home.js
const modalData = [
  { key: "添加经销商", navigateTo: "AddDealer" },
  { key: "添加特许经营", navigateTo: "AddFranchise" },
  { key: "新线索", navigateTo: "AddNewLeads" },
];

export default function Home({ navigation }) {
  const [modalVisible, setModalVisible] = useState(false);

  useEffect(() => {
    const unsubscribe = navigation.addListener("blur", () => {
      setModalVisible(false);
    });

    return unsubscribe;
  }, [navigation]);

  const renderPopList = ({ item, index }) => {
    const lastItem = index === modalData.length - 1;
    return (
      <TouchableOpacity onPress={() => navigation.navigate(item.navigateTo)}>
        <Text style={[styles.links, lastItem && styles.lastLink]}>
          {item.key}
        </Text>
      </TouchableOpacity>
    );
  };

  const toggleModal = () => {
    setModalVisible(!modalVisible);
  };

  return (
    <Modal
      animationType="slide"
      transparent={true}
      visible={modalVisible}
      onRequestClose={toggleModal}
      onTouchStart={toggleModal}
      style={styles.modalWrapper}
    >
      <TouchableWithoutFeedback onPress={toggleModal}>
        <View
          style={{ flex: 1, backgroundColor: "rgba(0,0,0,0.1)" }}
          onPress={toggleModal}
        ></View>
      </TouchableWithoutFeedback>
      <FlatList
        data={modalData}
        renderItem={renderPopList}
        onPress={toggleModal}
        style={[styles.modal]}
      />
    </Modal>
    //按钮位置
    <View style={styles.bottomBtnWrap}>
      <TouchableOpacity style={styles.addBtn} onPress={toggleModal}>
        <FontAwesome color="#fff" name="plus" size={30} />
      </TouchableOpacity>
    </View>
  );
}
英文:

you can use this code for it

//Home.js
const modalData = [
{ key: &quot;Add Dealer&quot;, navigateTo: &quot;AddDealer&quot; },
{ key: &quot;Add Franchise&quot;, navigateTo: &quot;AddFranchise&quot; },
{ key: &quot;New Lead&quot;, navigateTo: &quot;AddNewLeads&quot; },
];
export default function Home({ navigation }) {
const [modalVisible, setModalVisible] = useState(false);
useEffect(() =&gt; {
const unsubscribe = navigation.addListener(&quot;blur&quot;, () =&gt; {
setModalVisible(false);
});
return unsubscribe;
}, [navigation]);
const renderPopList = ({ item, index }) =&gt; {
const lastItem = index === modalData.length - 1;
return (
&lt;TouchableOpacity onPress={() =&gt; navigation.navigate(item.navigateTo)}&gt;
&lt;Text style={[styles.links, lastItem &amp;&amp; styles.lastLink]}&gt;
{item.key}
&lt;/Text&gt;
&lt;/TouchableOpacity&gt;
);
};
const toggleModal = () =&gt; {
setModalVisible(!modalVisible);
};
return (
&lt;Modal
animationType=&quot;slide&quot;
transparent={true}
visible={modalVisible}
onRequestClose={toggleModal}
onTouchStart={toggleModal}
style={styles.modalWrapper}
&gt;
&lt;TouchableWithoutFeedback onPress={toggleModal}&gt;
&lt;View
style={{ flex: 1, backgroundColor: &quot;rgba(0,0,0,0.1)&quot; }}
onPress={toggleModal}
&gt;&lt;/View&gt;
&lt;/TouchableWithoutFeedback&gt;
&lt;FlatList
data={modalData}
renderItem={renderPopList}
onPress={toggleModal}
style={[styles.modal]}
/&gt;
&lt;/Modal&gt;
//Button Here
&lt;View style={styles.bottomBtnWrap}&gt;
&lt;TouchableOpacity style={styles.addBtn} onPress={toggleModal}&gt;
&lt;FontAwesome color=&quot;#fff&quot; name=&quot;plus&quot; size={30} /&gt;
&lt;/TouchableOpacity&gt;
&lt;/View&gt;
);
}
</details>

huangapple
  • 本文由 发表于 2023年4月11日 15:14:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/75983307.html
匿名

发表评论

匿名网友

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

确定