【问题标题】:React native navigate to other screen反应原生导航到其他屏幕
【发布时间】:2021-08-12 18:28:30
【问题描述】:

我正在尝试重定向到 CallScreen,但未调用 redirect() 函数。我的代码在https://pastebin.com/FKLfGxhR,该函数应该被调用

  options={{
  'Choose From Library': () => {
    this.redirect();
  },
}} 

这里。

函数在类中,并且是

  redirect = () => {
this.props.navigation.navigate('CallScreen')
console.log('test')

}

【问题讨论】:

    标签: javascript react-native class react-native-gifted-chat


    【解决方案1】:

    renderActions 应该是类的方法。使其成为一种方法,您还必须使用this.renderActions 而不是renderActions

    class ChatScreen extends React.Component {
      renderActions = (props) => {
        return <Actions
          {...props}
          containerStyle={{
            width: 44,
            height: 44,
            alignItems: 'center',
            justifyContent: 'center',
            marginLeft: 4,
            marginRight: 4,
            marginBottom: 0,
          }}
          icon={() => (
            <Image
              style={{ width: 32, height: 32 }}
              source={{
                uri: 'https://placeimg.com/32/32/any',
              }}
            />
          )}
          options={{
            'Choose From Library': () => {
              this.redirect();
            },
          }}
          optionTintColor="#222B45"
        />
      };
        
      redirect = () => {
        this.props.navigation.navigate('CallScreen')
        console.log('test')
      }
      
      state = {
        messages: [],
      };
    
      get user() {
        const { name, id, avatar } = this.props.route.params;
        return {
          name: name,
          _id: id,
          avatar: avatar,
        };
      }
    
        componentDidMount() {
          FirebaseStorage.shared.on(message =>
            this.setState(previousState => ({
              messages: GiftedChat.append(previousState.messages, message),
            }))
          );
      }
      
      componentWillUnmount() {
          FirebaseStorage.shared.off();
      }
    
      render() {
        return <SafeAreaView style={{flex: 1}}>
            <GiftedChat
                  messages={this.state.messages}
                  onSend={FirebaseStorage.shared.send}
                  user={this.user}
                  alwaysShowSend
                  showUserAvatar
                  isAnimated
                  showAvatarForEveryMessage
                  renderBubble={this.renderBubble}
                  renderMessageImage={() => this.showImage}
                  renderUsernameOnMessage
                  isTyping
                  renderActions={this.renderActions}
            />
        </SafeAreaView>
      }
    }
    

    【讨论】:

      【解决方案2】:

      renderActions 应该在调用this 之前住在Component 中。

      class ChatScreen extends React.Component {
        
        //..your stuff
      
        const renderActions = (props) => {
          return <Actions
            ...props
            options={{
              'Choose From Library': () => {
                //It is safe to call this.XXX from here
                this.redirect();
              },
            }}
          />
        };
      
        render() {
          return <SafeAreaView style={{flex: 1}}>
              <GiftedChat
                    messages={this.state.messages}
                    onSend={FirebaseStorage.shared.send}
                    user={this.user}
                    alwaysShowSend
                    showUserAvatar
                    isAnimated
                    showAvatarForEveryMessage
                    renderBubble={this.renderBubble}
                    renderMessageImage={() => this.showImage}
                    renderUsernameOnMessage
                    isTyping
                    renderActions={renderActions}
              />
          </SafeAreaView>
        }
        redirect = () => {
          this.props.navigation.navigate('CallScreen')
          console.log('test')
        }
      }
      

      【讨论】:

      • 这不起作用...它给了我未定义的 renderActions。我删除了 const,因为它给了我一个错误。
      • 查看更新后的答案。请仔细检查我的意思。您只需在组件内定义renderActions,这样您就可以在this.xxxxscope 下
      • 我做不到。 const 不能在类中使用...@Karol A.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多