簡體   English   中英

我想調用另一個組件的方法

[英]I want to call a method of another component

我的組件AddPost.js使用machine.js中存在的機器,在上一個文件中,我想調用AddPost中存在的方法,我嘗試導入AddPost,但是它不起作用。 我有該錯誤:可能的未處理的承諾拒絕(id:0):ReferenceError:未定義addInBubble我想知道如何在machine.js中調用此方法,因為我在此文件中有操作,因此我將在行動

AddPost.js

import React, {PureComponent} from 'react';
import {View, Text, TextInput,ScrollView,StyleSheet} from 'react-native';
import {Button,Input,Bubble,ThemeProvider} from 'nachos-ui';
import {Navigation} from 'react-native-navigation';
import PropTypes from 'prop-types';
import {fetchMachine} from '../../helpers/machine.js';

console.disableYellowBox = true;
const styles = StyleSheet.create({
  container :{ 
    marginVertical:15 , flex: 1,
    flexDirection: 'column',
  },
  ButtonView : {
    flex: 1, 
    flexDirection: 'row',
    position: 'absolute',
    bottom:0,

  },
  bubbleStyle:{
     marginBottom: 15,
     marginLeft: 20,
     width: '80%'

    }
});

  let currentState = fetchMachine.initialState;
  let valuesCurrentState = currentState.nextEvents;
class AddPost extends PureComponent {

  static propTypes = {
    componentId: PropTypes.string,
    text:PropTypes.string
  }

  constructor(props) {
    super(props);
    this.state = {
      responseValue :[],
      value1 : 'Yes',
      value2 : 'No',
      value : ''

    };
  }


  addInBubble = () =>{
    let newValue = this.state.value;
    this.setState({      
      responseValue : [...this.state.responseValue,newValue]
    });
  }

  render() {

    let newArray = this.state.responseValue && this.state.responseValue.map(( item, key ) =>{
      return(
        <View key = { key } >
            <Bubble style = { styles.bubbleStyle }> { item }</Bubble>
        </View>
    );
    });
    return (

         <ThemeProvider>
        <View style={styles.container}>
        <ScrollView >
                <View style = {{ flex: 1, padding: 4 }}>
                {
                    newArray
                }
                </View>
        </ScrollView>
          <View style={styles.ButtonView}>
          <Button
              onPress = {this.addInBubble} 
          >
            {this.state.value1}
          </Button>
          <Button
              onPress = {this.addInBubble} 
          >
            {this.state.value2}
          </Button>
          </View>

        </View>
      </ThemeProvider>
    );
  }
}
export default AddPost;

machine.js

import { Machine, actions } from 'xstate';
import {AddPost} from '../screens/Summary/AddPost';

const fetchMachine = Machine({
  id: 'Hands Free',
  context: { attempts: 0},
  initial: 'summary',
  states: {
    summary: {
      on: {
          yes: {
            target: 'prioritization',
            actions: ['activate', 'sendTelemetry']
          },
        no : 'readMails'
      }
    },
    prioritization: {
      on: {
        read: 'readByPrioritization',
        skip: 'readMails'
      }
    },
    readByPrioritization: {

          on: {
        read: 'readMails',
        skip: 'listener'
      }

    },
    readMails: {
      on: {
        next: 'readMails',
        skip: 'listener'
      }
    },
    listener: {

      on: {
        received: 'readSummary',
        notyet: 'listener'
      }
    },
    readSummary: {

      on: {
        read: 'readEmail',
        skip :'listener'
      }
    },
    readEmail: {

      on: {
        finish: 'listener',
      }
    }
  }
},
  {
    actions: {
      // action implementations
      activate: (context, event) => {
        console.log('activating...');
          AddPost.addInBubble(); // this the methode that i want to call
        return 0;
      },
      notifyActive: (context, event) => {
        console.log('active!');
      },
      notifyInactive: (context, event) => {
        console.log('inactive!');
      },
      sendTelemetry: (context, event) => {
        console.log('time:', Date.now());
      }
    }
  });

  export{fetchMachine};

I want to call the method addInBubble but I have an error

您可以將parent方法作為道具傳遞給child組件,以便對其進行訪問

Class A extends Component {
  addInBubble = () => {
    // Some logic here
  }

  render() {
    <B addInBubble={this.addInBubble} />
  }

}

以便在組件B中訪問

Class B extends Component {

  render() {
    <div onClick={this.props.addInBubble} />
  }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM