[英]Referencing a component in a stateless React Native function
I'm using an Accordion component which takes in a function to render the content. 我正在使用手风琴组件,该组件具有一个功能来呈现内容。 In the content I'm using a Slider .
在内容中,我正在使用Slider 。 I'm trying to add a functionality to the Slider so you can also select a value by tapping a point in the bar.
我正在尝试向滑块添加功能,以便您还可以通过点击栏中的一个点来选择一个值。 However I'm not able to reference the slider view from outside since you cannot use refs within a stateless component.
但是,我无法从外部引用滑块视图,因为您不能在无状态组件中使用引用。 Here is what I'm trying to do:
这是我想要做的:
render() {
Return (
<Accordion
sections={SECTIONS}
renderHeader={this.renderHeader}
renderContent={this.renderContent}
/>
)
}
tapSliderHandler = (evt) => {
this.refs.slider.measure((fx, fy, width, height, px, py) => {
this.setState({value: (evt.nativeEvent.locationX - px) / width});
})
}
renderContent = (section, i, isActive) => {
return (
<View ref="slider" style={styles.container}>
<TouchableWithoutFeedback onPressIn={this.tapSliderHandler}>
<Slider
value={this.props.appState[section.id]}
onValueChange={value => this.props.appState[section.id] = value}
/>
</TouchableWithoutFeedback>
</View>
);
}
The problem is that I cannot reference the ref="slider" from tapSliderHandler since the function is stateless. 问题是我无法从tapSliderHandler引用ref =“ slider”,因为该函数是无状态的。 Any ideas on how to solve this?
关于如何解决这个问题的任何想法?
I found answers on how to achieve this by using classes instead of stateless functions, but I can't figure out how to do this when the accordion expects a function for rendering the content. 我找到了有关如何通过使用类而不是无状态函数来实现此目的的答案,但是当手风琴需要一个用于呈现内容的函数时,我不知道如何做到这一点。
The tapping code is following an example taken from here . 窃听代码遵循此处的示例。
Assuming your question is, "How to I call the renderContent
function and pass along the arguments (section, i, isActive)?" 假设您的问题是,“如何调用
renderContent
函数并传递参数(节,即isActive)?” then this is how you could do that. 那么这就是你可以做到的。
<Accordion
sections={SECTIONS}
renderHeader={this.renderHeader}
renderContent={this.renderContent.bind(null, section, i, isActive)}
/>
But I'm not positive that's the solution you're looking for. 但是我不太肯定这是您要寻找的解决方案。 If not, see my comment and help me understand where your code breaks down on you.
如果没有,请查看我的评论并帮助我了解您的代码在哪里出现了问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.