Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
150 views
in Technique[技术] by (71.8m points)

javascript - onClick only works if pressed twice

I have a modal with buttons inside, when these buttons are selected it is expected that it will sort the contact list according to what I want. (latest, oldest, readed, unread). But the list only changes when I click the button twice.

This is my modal component

export const Options = ['Mais recente', 'Mais antigos', 'Lidos', 'Nao lidos']; //array with options

const ModalOrder = ({modalIsOpen, setModalIsOpen, orderContacts, isOrder, setIsOrder}) => { //modal component

  return (
    <>  
    <Overlay display={modalIsOpen ? "flex" : "none"} onClick={() => setModalIsOpen(s => !s)} >
    </Overlay>
      <Wrapper height={modalIsOpen} shadow="0px -4px 4px rgba(0, 0, 0, 0.1);" display={"flex"} margin="10px 0 0 0" align="center" justify="center" zIndex="2011">
        {/* <Row transform={modalIsOpen ? "translateY(0)" : "translateY(327px)"} cursor="pointer" onClick={() => setModalIsOpen(s => !s)} custom="bottom: 0; position: absolute; margin-bottom: 320px; transition: all ease 0.3s;" width="90px" height="6px" background="#FFF9F9" radius="100px" shadow="0px 4px 4px rgba(0, 0, 0, 0.1);"></Row> */}
        <Icon custom="bottom: 0; position: absolute; margin-bottom: 310px; transition: all ease 0.3s;" transform={modalIsOpen ? "translateY(0)" : "translateY(360px)!important"} onClick={() => setModalIsOpen(s => !s)}/>
        <Column transform={modalIsOpen ? "translateY(0)" : "translateY(309px)"} align="center"  custom="bottom: 0; position: absolute; border-top-left-radius: 35px; border-top-right-radius: 35px; transition: all ease 0.3s;" width="100%" height="308px" background="#FFF" position="absolute">
          <Text margin="16px 0 12px 0" font="bold 16px/21px Segoe UI Regular" color="#448757" >Ordenar</Text>
          
          { 
            Options.map(content => {

              const handleOrder = () => {
                setIsOrder(content)
              
                orderContacts()
              }
              
              //executa o mais recente como ja selecionado
              useEffect(() => {
                setIsOrder("Mais recente")
                
              }, [])

              return <ButtonOpt 
                // orderContacts={orderContacts} 
                key={content} 
                txt={content} 
                isSelected={isOrder === content} 
                // setIsSelected={() => setIsOrder(content)} 
                onClick={handleOrder}
                borderT={content === "Mais recente" ? "1px solid rgb(196, 196, 196, 0.8)" : "none"} 
              />
            })
          }
          
        </Column>
      </Wrapper>
    </>
  );
};

const ButtonOpt = ({borderT, isSelected, txt, onClick}) => { //Modal button component

  return (
    <Button borderT={borderT || "1px solid rgb(196, 196, 196, 0.8)"} borderB={"1px solid rgb(196, 196, 196, 0.8)"} onClick={onClick} height="32px" width="100%" align="center" justify="start"> 
      <Row shadow="2px 0px 4px rgba(0, 0, 0, 0.1);" display={isSelected ? "flex" : "none"}  custom="border-bottom-right-radius: 5px; border-top-right-radius: 5px; position: absolute; left: 0;" width="10px" height="32px" background="#448757" /> 
      <Text margin="0 0 0 20px" font="normal 14px/19px Segoe UI Regular" color="#525252" >{txt}</Text> 
    </Button>
  )
}

export default ModalOrder;

I use a useState to store the state of each button when clicked (that isOrder).

That is my verification code:

contactsArr.sort(function (a, b) {
            //a.lastMessage.readBy
            if( isOrder === "Mais recente" || isOrder === null){
                console.log(contactsArr.sort, "array sort - case latest")
                return (a.lastMessage.sentAt > b.lastMessage.sentAt) ? -1 : ((a.lastMessage.sentAt < b.lastMessage.sentAt) ? 1 : 0);
            }
            if( isOrder === "Mais antigos"){
                console.log(contactsArr.sort, "array sort - case oldest")
                return (b.lastMessage.sentAt > a.lastMessage.sentAt) ? -1 : ((b.lastMessage.sentAt < a.lastMessage.sentAt) ? 1 : 0);
            }
            if( isOrder === "Lidos"){
                console.log(contactsArr.sort, "array sort - case readed")
                return (a.lastMessage.readBy > b.lastMessage.readBy) ? -1 : ((a.lastMessage.readBy < b.lastMessage.readBy) ? 1 : 0);
            }
            if( isOrder === "Nao lidos"){
                console.log(contactsArr.sort, "array sort - case unread")
                return (b.lastMessage.readBy > a.lastMessage.readBy) ? -1 : ((b.lastMessage.readBy < a.lastMessage.readBy) ? 1 : 0);
            }

Work well, but only if i click twice.

So i console log the ifs, so i can see whats happens, and i console log the handle to see if it is running.

So, when the aplication start that console.log appears:

? sort() { [native code] } "array sort - case latest"

Because i create a useEffect for the application to start with the latest selected.

So i open the modal:

enter image description here

And click in the second option (oldest). And this console.log appears:

? sort() { [native code] } "array sort - case latest"

And when i click again this appears:

? sort() { [native code] } "array sort - case oldest"

And the listing works normally. One thing I noticed is that if I click the oldest first and then the latest, the list will sort with the oldest. it is as if it saved the first click and showed the current one, but only executed it in the second.

question from:https://stackoverflow.com/questions/65938901/onclick-only-works-if-pressed-twice

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...