为什么子组件在React Native中没有从父组件接收更新的值?

仙女皇后

单击箭头后,“购物车项目视图”需要扩展该特定视图并折叠当前展开的所有其他视图。该产品的商品ID将传递到父组件,以更新要扩展(活动)的视图。尽管正在传递ID并将其设置在化简器中的expandedItem属性上,但它不会更新到子组件(即使它是作为prop在子组件上传递的)。最后,重新评估子组件时,expandedViewItem仍为0,这是其默认值。有谁知道如何获取子组件以接收更新的expandedItem值?为什么仍然是0?

请观看我调试此问题的视频:https : //youtu.be/qEgxqAyWDpY

这是在子组件中评估值的位置

render () {

const isExpanded = product.id === this.props.expandedViewId;

这是整个子组件类:

export default class CartProductItem extends Component {
    constructor(props) {
        super(props);
        this.state = {showCounter: false};
    }

    expandCartProductItem(id) {
        this.props.onExpandClick(id); 
        this.setState(this.state);
    }

    updateDisplay = (nextProps) => {
        // Null check is needed here as 0 is a valid value
        if (nextProps.activeIndex !== null && nextProps.activeIndex === this.props.index) {
            this.setState({
                showCounter: !this.state.showCounter
            });
        } else if (nextProps.activeIndex !== null && nextProps.activeIndex !== this.props.index) {
            this.setState({
                showCounter: false
            });
        }
    }

    componentWillReceiveProps(nextProps) {
        console.log('nextProps: ', nextProps)
    }

    render() {
        const serverUrl = getServerAddress();
        const {product} = this.props;
        const price = product.iogmodPrice ? product.iogmodPrice : product.price;

        const isExpanded = product.id === this.props.expandedViewId;

        const imageSrc = product.imageName
            ? 'https://b2b.martinsmart.com/productimages/'+ product.imageName.replace("Original", "Thumbnail")
            : serverUrl + '/b2b/resources/images/nophoto.gif';

        return (
            <View style={styles.pContainer}>
                <CartProduct
                    imageName={imageSrc}
                    name={product.description}
                    itemNum={product.id}
                    price={price}
                    pack={product.pack}
                    averageWeight={product.averageWeight}
                    cases={product.order_count}
                />

                <TouchableOpacity style={styles.buttonContainer} onPress={() => this.expandCartProductItem(product.id)}>
                    {isExpanded ? <LineIcon name="arrow-up" style={styles.arrowIcon} /> : <LineIcon name="arrow-down" style={styles.arrowIcon} />}
                </TouchableOpacity>

                {isExpanded &&
                    <ExpandHeightView height={70}>
                        <View style={styles.counterContainerView}>
                            <QuantityCounter style={{width: '100%'}} product={product} />
                        </View>
                    </ExpandHeightView>
                }
            </View>
        );
    }
}

这是将id传递给action以及渲染器中的初始const声明父组件函数:

expandAndCollapseItems = (id) => {
    this.props.dispatch(collapseCartItemViews(id));
}

render() {
    const {isLoading, displayDetails, sortCasesAsc, details, items, expandedItem} = this.props.orderInfo;

这是父组件中的子组件,将expandedItem变量传递到该子组件中

<FlatList 
    data={items}
    keyExtractor={(item, index) => item.id.toString()}
    renderItem={({item}) => <CartProductItem product={item} expandedViewId={expandedItem} onExpandClick={(id) => this.expandAndCollapseItems(id)} />}
/>

最后,这是reducer函数更新应用程序状态的方法:

    case types.SET_EXPANDED_STATE:
        const id = action.id;

        return {
            ...state,
            expandedItem: id
        }
马特·阿夫特

由于您使用的是redux,因此您只需从子级的redux存储中获取扩展的商品ID,而无需将其从父级传递下来。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么子组件不随父状态更新?

为什么子组件的prop只显示父组件的初始值?

如何从父组件中的表单接收值?

当我从父组件更改其状态时,为什么我的子组件没有更新?

为什么react的UI组件没有更新?

Reactjs:为什么子组件不会在数据更改时重新呈现?

(Blazor) 为什么子剃须刀组件不起作用?

为什么反应功能组件中的回调没有读取更新的状态值

为什么我的 React Function 组件没有出现?

为什么组件没有从服务接收数据

为什么要在React(Native)中扩展组件?

为什么 React 组件不渲染?

如果 props 和 state 没有改变,为什么 React 组件会更新

为什么我的 React 组件没有更新图片库?

为什么子进程“ sleep 10”没有终止?

React js,当我更改open的值时,为什么我的组件没有重新渲染?

为什么即使我没有向组件传递任何内容,React仍会渲染组件?

为什么 React 组件类和功能组件没有相同的行为?

在 react-redux 中从父组件更新子组件的状态

为什么我的组件没有从另一个组件接收数据?

我在 React 子组件中的函数的参数没有被传递给父组件的函数。为什么会这样?

为什么我的样式没有在React Native中更新?

更新React组件而没有父组件

当提供程序组件的父状态更改时,为什么不能更新子组件中的值(带有react上下文)?

为什么我的react组件中的context.router没有定义?

用 Enzyme 开玩笑,为什么在我的 React 组件测试中没有触发“toHaveBeenCalled”?

为什么在 React 中,当父组件重新渲染时,子组件不会重新渲染(子组件没有被 React.memo 包裹)?

为什么React Native <Text>组件不显示布尔属性?

为什么我的 React Native 组件不断刷新?