简体   繁体   English

在子组件和父组件之间进行通信以更改react-native中的Parent的视图内容的正确方法是什么?

[英]What is the right way to communicate between child and parent component to change view content in Parent in react-native?

I have a child component where I have a button. 我有一个带有按钮的子组件。

Now I have three of these child components placed next to each other in one Parent component. 现在,我在一个父级组件中将三个子级组件彼此相邻放置。

Whenever any of these child comp is touched, I wish to change stuff in my parent component. 每当触摸这些子组件中的任何一个时,我都希望更改父组件中的内容。 So whenever the button is clicked, I wish to change state (or whatever is the way to do it) in parent and then go on and change the list that. 因此,无论何时单击该按钮,我都希望更改父项的状态(或执行此操作的任何方式),然后继续进行操作,并更改其列表。 I am loading in another child component used in parent. 我正在加载用于父级的另一个子级组件。 But when I press those buttons, my app just freezes after 3-4 clicks every time. 但是,当我按下这些按钮时,我的应用程序每次都只有3-4次点击后冻结

I thought maybe I am doing something very basic wrong. 我以为也许我在做一些非常基本的错误。 Like using the state/prop wrong and sending the app to infinite loop or something. 就像错误地使用状态/属性并将应用发送到无限循环之类。

Parent Component: 父组件:

export default class Dash extends PureComponent {
constructor(){
    super();
    // Bind the this context to the handler function
    this.state = {
        activeAuctionsCount: 15,
        wonAuctionsCount: 10,
        convertedLeadsCount: 6,
        isActiveAuctionsSelected: true,
        iswonAuctionsSelected: false,
        isconvertedLeadsSelected: false,
        cardSelected: 'auctions', /* auctions/won/leads */
    };
    this.loadActiveAuctions = this.loadActiveAuctions.bind(this);
    this.loadWonAuctions = this.loadWonAuctions.bind(this);
    this.loadconvertedLeads = this.loadconvertedLeads.bind(this);
}

// This method will be sent to the child component
loadActiveAuctions() {
    console.log('active pressed');
    this.setState({
        cardSelected: 'auctions'
    });
}
loadWonAuctions() {
    console.log('won pressed');
    this.setState({
        cardSelected: 'won'
    });
}
loadconvertedLeads() {
    console.log('leads pressed');
    this.setState({
        cardSelected: 'leads'
    });
}

render() {
    return (
                            <DealerShipDashStatsCard 
                            statCardLayoutPath={statCardLeft}
                            statCardTitle={'NOW'+"\n"+'SHOWING'}
                            statValue={this.state.activeAuctionsCount}
                            isSelected={this.state.isActiveAuctionsSelected} 
                            action={this.loadActiveAuctions}
                            />                         
                        </View>

Child Component: 子组件:

export default class DealershipDash_StatsCard extends Component {
  render() {
    console.log("Rendering DashStat Card "+ this.props.statCardTitle);
    return (
      <ImageBackground 
      source={this.props.statCardLayoutPath} 
      style={styles.stat_card} 
      resizeMode="cover"
      resizeMethod="resize">
        <View style={styles.cardTop}></View>
        <View style={styles.cardBottom}>
            <View style={styles.cardStatTitle}>
                <Text style={[styles.statTitleText]}>{this.props.statCardTitle}</Text>
            </View>
            <View style={styles.cardStatValue}>
                <Text style={styles.cardStatValueText}>{this.props.statValue}</Text>
            </View>
            <View style={styles.cardButton}>
                <Image 
                source={this.props.isSelected ? cardButtonActive : cardButtonInactive } 
                style = {this.props.isSelected ? styles.stat_card_button : styles.stat_card_button_inactive}/>
            </View>
        </View>
        <Button onPress={this.props.action} title="press"/>
      </ImageBackground>
    );
  }
}

Is there anything I'm doing wrong or not the react way? 我有做错什么还是没有反应的方法吗? (this is my First project with react) (这是我的第一个反应项目)

I am also sometimes getting Possible EventEmitter memory leak detected warning in my metro bundler. 有时我的地铁捆绑Possible EventEmitter memory leak detected也可能收到“ Possible EventEmitter memory leak detected警告”。 When i check console, I see everything is getting re-rendered on every click, maybe that's making it so slow that it finally gives up? 当我检查控制台时,我发现每次单击都重新呈现所有内容,也许这是如此之慢以至于最终放弃了?

一切看起来都不错,但是如果为孩子添加默认道具,其他开发人员将更容易理解。

I solved this problem, eventually. 我终于解决了这个问题。 I was not using PureComponent for the header component that had a ticking clock. 我没有将PureComponent用于具有刻度时钟的标头组件。 This made the header (with a few images inside) re-render entirely every second. 这使得标头(其中包含一些图像)完全每秒重新呈现。 As a result, JS frame rate for my app was dropping too low, hence the freezing. 结果,我的应用程序的JS帧速率下降得太低,因此冻结了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在React Native中的组件之间进行通信(子级 - >父级) - Communicate Between Components in React Native (child -> parent) React:父子组件之间的通信 - React: communicate between parent and child component react-Native:在父组件内部访问子组件函数 - react-Native: Access child component function inside parent component 在React.js中的父组件和子组件之间进行通信 - Communicate between parent and child component in React.js 如何在本机与子代之间传递数据? - How to pass data between child and parent in react-native? 有没有一种方法可以将Text组件的全部内容包装在react-native的父视图中? - Is there a way I can wrap the whole contents of a Text component inside parent view in react-native? 从反应和本机反应的意义上来说,父组件和子组件是什么意思? - What is meant by parent component and child component in the sense of react and react native? 有没有办法从 React Native 的子组件更改父组件上文本元素的文本颜色? - Is there a way to change text colour of a text element on a parent component from the child component in React Native? 如何在React Native中从子组件更改父状态? - How to change parent's state from child component in react native? 通过函数参数将属性从动态子组件传递到父功能组件 react-native - Passing properties from a dynamic Child component to a Parent functional component through a functions parameters react-native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM