简体   繁体   English

从父组件更新子组件道具反应原生

[英]Update child component props from Parent component react native

I want to update child component props from Parent component my senarios is I have one component which I am passing one array list which is come from API response so below is my code我想从父组件更新子组件道具我的场景是我有一个组件,我正在传递一个来自 API 响应的数组列表,所以下面是我的代码

                    <DateRangePicker
                        theme={{
                            calendarBackground: colors.white,
                            selectedDayBackgroundColor: colors.kellyGreen,
                            selectedDayTextColor: colors.white,
                            todayTextColor: colors.kellyGreen,
                            dayTextColor: colors.intrestedButton,
                            dotColor: colors.kellyGreen,
                            selectedDotColor: colors.kellyGreen,
                            arrowColor: colors.kellyGreen,
                            monthTextColor: colors.black,
                            textDayFontFamily: globals.SFProTextRegular,
                            textMonthFontFamily: globals.SFProTextMedium,
                            textDayHeaderFontFamily: globals.SFProTextMedium,
                            textMonthFontWeight: "bold",
                            textDayFontSize: globals.font_11,
                            textMonthFontSize: globals.font_16,
                            textDayHeaderFontSize: globals.font_13
                        }}
                        minDate={null}
                        isFrom={'golfActivity'}
                        monthFormat={globals.selectedLocal.DATE_MMMMyyyy}
                        initialRange={[this.state.fromDate, this.state.toDate]}
                        onSuccess={(s, e) => this.setState({ fromDate: e, toDate: s })}
                        theme={{ markColor: colors.kellyGreen, markTextColor: colors.white 
                        }}
                        underLineValue = {this.state.underLineValue} 
                        onVisibleMonthsChange={months => { this.getChangeMonth(months) }}
                        />

in above code underLineValue is my array list which is come from API side and when I change month at that time i onVisibleMonthsChange props is called and I get newly updated month and year so again I am calling API for that and fill new my updated array refer my getChangeMonth method as below在上面的代码中,LineValue 是我的数组列表,它来自 API 方面,当我改变月份时,我调用了 onVisibleMonthsChange 道具,我得到了新更新的月份和年份,所以我再次调用 API 并填写新的我更新的数组引用我的 getChangeMonth 方法如下

getChangeMonth = (months) => {
    countCall = countCall + 1
    if (countCall === 1) {
        this.setState({isMonthChange: true})
        visibleMonth = months[months.length - 1].month;
        visibleYear = months[months.length - 1].year;
        globals.visibleMonth= visibleMonth;
        globals.visibleYear= visibleYear;
        console.log("on visible month", visibleMonth);
        console.log("on visible year", visibleYear);
        this.callCounterAPI(visibleMonth, visibleYear); 
        countCall = - 1
    }
}

callCounterAPI(month, year){
    this.setState({ underLineValue: []})
    API.getCalendarCount(this.onResponseCalendarCount, month, year,true)
}

onResponseCalendarCount = {
    success: (response) => {
          this.setState({underLineValue: response.data })
    },
    error: (err) => {
        console.log("onResponseCalendarCount error-->>", err);

    },
    complete: () => {
    }
}

export default class DateRangePicker extends Component<Props> {
 state = { isFromDatePicked: false, isToDatePicked: false, markedDates: {} }

 componentDidMount() {
   console.log("DateRangePicker-->"+ JSON.stringify(this.props));

   }
}

onResponseCalendarCount callback I fill updated arraylist underLineValue but in DateRangePicker when i print it's props I did't get updated arraylist so any one have idea how can i solve this issue? onResponseCalendarCount 回调我填写更新的 arraylist underLineValue 但是在 DateRangePicker 当我打印它的道具时我没有得到更新的 arraylist 所以有人知道我该如何解决这个问题? Your all suggestions are welcome欢迎您提出所有建议

You can use getDerivedStateFromProps method in child component like this:您可以在子组件中使用 getDerivedStateFromProps 方法,如下所示:

 import isEqual from 'lodash/isEqual'

   static getDerivedStateFromProps(props, state) {
    if (!isEqual(props.underLineValue, state.underLineValue)) {
        return {
            underLineValue: props.underLineValue
        }
    }
    return null;
}

This will update your child component.这将更新您的子组件。 Let me know if it's working.让我知道它是否有效。

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

相关问题 如何使用 React Native 中的 props 将数组从父组件传递到子组件? - How to pass an array from a parent component to child component using props in React Native? 在反应中将道具从父组件传递给子组件 - Passing props from a parent component to a child component in react React父级组件道具未传递给子级组件 - React parent component props are not passing to child component React:子组件过滤父组件道具 - React: Child Component Filtering Parent Component Props React Native:将多个父项props传递给子组件 - React native: passing in multiple parent props to child component 将道具从父级发送到子级,并在子级组件(ReactJs)中对其进行更新 - Send props from parent to child and update them in child component (ReactJs) 在React中将状态从子组件更新为父组件 - Update the state from child to parent component in React 我们可以在 React 中将 props 从 Parent 传递给 Child 组件,并将 props 设置为子组件的 state 吗? - Can we pass props from Parent to Child component in React, and set the props as state of child component? 无法从 React Native 中的父组件更新子组件的属性 - Can’t update the property of a child component from a parent component in React Native 将 SetState 从父组件传递给 React Native 中的子组件 - Pass SetState from parent component to a child component in React Native
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM