English 中文(简体)
1. 在React原住民的窗户底层开始观察
原标题:Initialize view at bottom of the window in React Native

我正在与React原住民一道开展聊天室项目。 因此,我希望我的最新信息能够在底层和每当页重载时显示,滚动屏障将首先在底层,用户可以从底层向上滚。

我可以通过以下途径这样做:maxHala: 100dvhflexDirection:一栏逆 with React and CSS on the web,但我可以在网上与Reactindigenous实现同一行为。

The React + CSS demo: https://codesand Box.io/s/chat-room-web-clxqz2?file=/src/App.tsx

https://snack.expo.dev/@snow_yang/chat-room” rel=“nofollow noreferer”>。

如果你们当中有些人有任何想法,那就是一个很大的希望!

问题回答

如果你想把<代码>FlatList的底线滚动,你可尝试使用<代码>scrollToEnd( ref。 在<代码>FlatList之后,或者在添加新电文后,你可以使用这种方法。 您也可使用推进代码<>onContentSizeChange,以便在内容大小发生变化时启动滚动。

例如:

import React, { useEffect, useRef } from  react ;
import { View, Text, FlatList, StyleSheet } from  react-native ;

const MessageView = () => {
  const messages = [...];

  const flatListRef = useRef();

  useEffect(() => {
    if (flatListRef.current) {
      flatListRef.current.scrollToEnd({ animated: false });
    }
  }, []);

  const renderItem = ({ item }) => {
    return (
      <View style={styles.messageContainer}>
        <Text style={styles.userName}>{item._id}</Text>
        <Text style={styles.userName}>{item.user.name}</Text>
        <Text style={styles.messageText}>{item.text}</Text>
      </View>
    );
  };

  return (
    <View style={styles.container}>
      <FlatList
        ref={flatListRef}
        data={messages}
        keyExtractor={(item) => item._id}
        renderItem={renderItem}
        onContentSizeChange={() => flatListRef.current.scrollToEnd()}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor:  #f5f5f5 
  },
  messageContainer: {
    padding: 10,
    marginVertical: 5,
    marginHorizontal: 10,
    borderRadius: 8,
    backgroundColor:  #ffffff ,
  },
  userName: {
    fontWeight:  bold ,
    marginBottom: 5,
  },
  messageText: {
    fontSize: 16,
  },
});

export default MessageView;




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签