如何按日期对组件进行排序 [ REACT ]

发布于 01-19 06:45 字数 4233 浏览 2 评论 0原文

我已经发布了我的react - firebase 项目,我想按日期对组件消息和图像进行排序。所以我通过 map() 显示它,它按组件按日期排序,但它是分开的。我需要它按日期进行聚合和排序。如有任何帮助,我们将不胜感激。在此处输入图像描述

组件 ChatWindow

           <ContentStyled>
            <MessageListStyled ref={messageListRef}>
              {messages.map((mes) => (
                <Message
                  key={mes.id}
                  text={mes.text}
                  photoURL={mes.photoURL}
                  displayName={mes.displayName}
                  createdAt={mes.createdAt}
                  files={mes.files}
                  nameFile={mes.nameFile}
                />
              ))}
              {images.map((image) => (
                <Image 
                key={image.id}
                files= {image.files}
                nameFile= {image.name}
                createdAt={image.createdAt}
                photoURL={image.photoURL}
                displayName={image.displayName}
                />
              ))}

              <div>
              { messagesss }
              {imagesss}
              {/* {mergeds} */}
              </div>

            </MessageListStyled>
            {/* from upload images */}
            <FormImage>
              <form onSubmit={formHandler} className="form-image" >
                <div class="file-input">
                  <input
                    type="file"
                    name="file-input"
                    id="file-input"
                    class="file-input__input"
                  />
                  <label class="file-input__label" for="file-input">
                  <FileImageOutlined />
                  </label>
                  <button type="submit"><UploadOutlined /></button>
                </div>
              </form>
              {/* from upload messages */}
              <FormStyled style={{width: "96%"}} form={form}>
                <Form.Item name='message'>
                  <TextArea
                    ref={inputRef}
                    onChange={handleInputChange}
                    onSubmit={handleOnSubmit}
                    // onPressEnter={handleOnSubmit} 
                    placeholder='Nhập tin nhắn...'
                    bordered={false}
                    autoComplete='off'
                    autoSize={{ minRows: 1, maxRows: 4 }}
                  />
                </Form.Item>
                <Button type='primary'onClick={handleOnSubmit} style={{background: "#36393F", border: "none", fontSize: "1.2rem", marginBottom: "12px"}} >
                  <SendOutlined />
                </Button>
              </FormStyled>
            </FormImage>
          </ContentStyled>

组件消息

     <WrapperStyled className='hover'>
      <div>
        <Avatar size='small' src={photoURL}>
          {photoURL ? '' : displayName?.charAt(0)?.toUpperCase()}
        </Avatar>
        <Typography.Text className='author'>{displayName} 
      </Typography.Text>
        <Typography.Text className='date'>
          {formatDate(createdAt?.seconds)}
        </Typography.Text>
      </div>
      <div>
          <Typography.Text className='content'>{text} 
          </Typography.Text>
      </div>
    </WrapperStyled>

组件图像

    <WrapperStyled className='hover'>
      <div>
        <Avatar size='small' src={photoURL}>
          {photoURL ? '' : displayName?.charAt(0)?.toUpperCase()}
        </Avatar>
        <Typography.Text className='author'>{displayName}</Typography.Text>
        <Typography.Text className='date'>
          {formatDate(createdAt?.seconds)}
        </Typography.Text>
      </div>
      <div>
        {/* <Typography.Text className='content'>{text}</Typography.Text> */}
        <img src={files} alt={nameFile} className='content' />
      </div>
    </WrapperStyled>

I have issue my react - firebase project, I want sort component Message and Image by date. So i show it by map(), It's sorted by date by component but it's separate. I need it to aggregate and sort by date.Any help would be appreciated.enter image description here

Component ChatWindow

           <ContentStyled>
            <MessageListStyled ref={messageListRef}>
              {messages.map((mes) => (
                <Message
                  key={mes.id}
                  text={mes.text}
                  photoURL={mes.photoURL}
                  displayName={mes.displayName}
                  createdAt={mes.createdAt}
                  files={mes.files}
                  nameFile={mes.nameFile}
                />
              ))}
              {images.map((image) => (
                <Image 
                key={image.id}
                files= {image.files}
                nameFile= {image.name}
                createdAt={image.createdAt}
                photoURL={image.photoURL}
                displayName={image.displayName}
                />
              ))}

              <div>
              { messagesss }
              {imagesss}
              {/* {mergeds} */}
              </div>

            </MessageListStyled>
            {/* from upload images */}
            <FormImage>
              <form onSubmit={formHandler} className="form-image" >
                <div class="file-input">
                  <input
                    type="file"
                    name="file-input"
                    id="file-input"
                    class="file-input__input"
                  />
                  <label class="file-input__label" for="file-input">
                  <FileImageOutlined />
                  </label>
                  <button type="submit"><UploadOutlined /></button>
                </div>
              </form>
              {/* from upload messages */}
              <FormStyled style={{width: "96%"}} form={form}>
                <Form.Item name='message'>
                  <TextArea
                    ref={inputRef}
                    onChange={handleInputChange}
                    onSubmit={handleOnSubmit}
                    // onPressEnter={handleOnSubmit} 
                    placeholder='Nhập tin nhắn...'
                    bordered={false}
                    autoComplete='off'
                    autoSize={{ minRows: 1, maxRows: 4 }}
                  />
                </Form.Item>
                <Button type='primary'onClick={handleOnSubmit} style={{background: "#36393F", border: "none", fontSize: "1.2rem", marginBottom: "12px"}} >
                  <SendOutlined />
                </Button>
              </FormStyled>
            </FormImage>
          </ContentStyled>

Component Messages

     <WrapperStyled className='hover'>
      <div>
        <Avatar size='small' src={photoURL}>
          {photoURL ? '' : displayName?.charAt(0)?.toUpperCase()}
        </Avatar>
        <Typography.Text className='author'>{displayName} 
      </Typography.Text>
        <Typography.Text className='date'>
          {formatDate(createdAt?.seconds)}
        </Typography.Text>
      </div>
      <div>
          <Typography.Text className='content'>{text} 
          </Typography.Text>
      </div>
    </WrapperStyled>

Component Image

    <WrapperStyled className='hover'>
      <div>
        <Avatar size='small' src={photoURL}>
          {photoURL ? '' : displayName?.charAt(0)?.toUpperCase()}
        </Avatar>
        <Typography.Text className='author'>{displayName}</Typography.Text>
        <Typography.Text className='date'>
          {formatDate(createdAt?.seconds)}
        </Typography.Text>
      </div>
      <div>
        {/* <Typography.Text className='content'>{text}</Typography.Text> */}
        <img src={files} alt={nameFile} className='content' />
      </div>
    </WrapperStyled>

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幽蝶幻影2025-01-26 06:45:17

哦,我认为解决方案。我将它们分组为一个数组,然后使用Concat()合并它们。网络使用sort()进行排序。让我收到有关上述解决方案的闪电。全部。

const toArr = [(messages.map((mes) => ( // Group 2 component
    <Message
      key={mes.id}
      text={mes.text}
      photoURL={mes.photoURL}
      displayName={mes.displayName}
      createdAt={mes.createdAt}
      files={mes.files}
      nameFile={mes.nameFile}
    />
  )))
  ,
  (images.map((image) => (
    <Image 
    key={image.id}
    files= {image.files}
    nameFile= {image.name}
    createdAt={image.createdAt}
    photoURL={image.photoURL}
    displayName={image.displayName}
    />
  )))]

  const mergeds = toArr[0].concat(toArr[1]); //merge them
  const sortCreatedAt = mergeds.sort((a, b) => a.props.createdAt - b.props.createdAt); //Sort by createdAt

Oh, I think of a solution.I group them into an array, then use concat() to merge them.finally use sort() to sort. Let me receive lightning about the above solution. Thx all.

const toArr = [(messages.map((mes) => ( // Group 2 component
    <Message
      key={mes.id}
      text={mes.text}
      photoURL={mes.photoURL}
      displayName={mes.displayName}
      createdAt={mes.createdAt}
      files={mes.files}
      nameFile={mes.nameFile}
    />
  )))
  ,
  (images.map((image) => (
    <Image 
    key={image.id}
    files= {image.files}
    nameFile= {image.name}
    createdAt={image.createdAt}
    photoURL={image.photoURL}
    displayName={image.displayName}
    />
  )))]

  const mergeds = toArr[0].concat(toArr[1]); //merge them
  const sortCreatedAt = mergeds.sort((a, b) => a.props.createdAt - b.props.createdAt); //Sort by createdAt
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文