[英]React-native - How to create a infinite picker
I would like to create a infinite/loop react-native Picker like on the image below.我想创建一个无限/循环 react-native Picker,如下图所示。
So, my question is: When I'm scrolling, how can I make the Picker start again from the first item after reach out the last item?所以,我的问题是:当我滚动时,如何让 Picker 在到达最后一项后从第一项重新开始?
Here's my code:这是我的代码:
render() {
const hourItems = [];
for(var i = 0; i < 24; i++) {
hourItems.push(
<Picker.Item label={i.toString()} value={i} key={i} />
);
}
return(
<ScrollView style={styles.panel}>
<Picker
selectedValue={this.state.hour}
onValueChange={(itemValue, itemIndex) => this.setState({ hour: itemValue })}
>
{hourItems}
</Picker>
</ScrollView>
);
}
Create redux action which increment value of the minutes and hours.创建 redux 操作,增加分钟和小时的值。 Inside that action you can check whether it should be reset, for example:
在该操作中,您可以检查是否应该重置它,例如:
export const tick = () => (dispatch, getState) => {
const { minute } = getState().clock
if (minute === 59) {
dispatch({ type: RESET_MINUTE})
dispatch({ type: INCREMENT_HOUR})
} else {
dispatch({ type: INCREMENT_MINUTE})
}
}
and in the reducer:并在减速器中:
const createReducer = (initialState, handlers) =>
(state = initialState, action) => {
if (handlers.hasOwnProperty(action.type)) {
return handlers[action.type](state, action)
}
return state
}
export default createReducer(initialState, {
INCREMENT_HOUR(state, action) {
return {
...state,
hour: state.hour + 1,
}
},
INCREMENT_MINUTE(state, action) {
return {
...state,
minute: state.minute + 1,
}
},
RESET_MINUTE(state) {
return {
...state,
minute: 0,
}
},
})
then connect your component with the reducer and the update should be automatic :)然后将您的组件与减速器连接起来,更新应该是自动的:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.