React Context: TypeError: render is not a function

前端 未结 1 994
渐次进展
渐次进展 2021-01-01 14:11

I\'m trying to use React Context to pass a function to a nested child component, which effectively allows the child to update the parents state when pressed.

The pro

相关标签:
1条回答
  • 2021-01-01 14:44

    As the error states, <Consumer> should have the only child, and it should be a function. The error appears when it has multiple children, including text nodes.

    ; after embedded expression causes the problem. It's not a part of an expression, and making it a part of it would result in syntax error.

    It should be:

    <IndexContext.Consumer>
        {({ handleChange }) => (
            <TouchableOpacity onPress={handleChange(props.index)}>
                <AsyncImage
                    source={ props.img_src }
                    placeholderColor="#fafafa"
                    style={{ flex: 1, width: null, height: 200 }}
                />
                <Text>{ props.var_name }</Text>
                <Text>{ props.price }</Text>
                <Text>{ props.sku }</Text>
            </TouchableOpacity>
        )}
    </IndexContext.Consumer>
    
    0 讨论(0)
提交回复
热议问题