简体   繁体   English

无状态组件中的状态替代

[英]Alternative of state in stateless component

enter code here I was trying to include the Wheel Picker component to my project. enter code here我试图将Wheel Picker组件包含到我的项目中。 It works well when cover it in the stateful component.So I tried convert it to stateless component, when I scroll the wheel, the selected Index always index to default value( Ex: when I scroll to item number 10, and the wheel auto scroll back to default 0) although I receive the correct values. 当将其覆盖在有状态组件中时效果很好。因此,我尝试将其转换为无状态组件,当滚动滚轮时,选定的索引始终索引为默认值(例如:当我滚动到项目编号10时,滚轮会自动滚动返回默认值0),尽管我收到了正确的值。 Is there something wrong with my component? 我的组件有问题吗?

import React, {PropTypes} from 'react'

import {   
    Component,
    StyleSheet,
    Text,
    View
} from 'react-native';

import Picker from 'react-native-wheel-picker'
var PickerItem = Picker.Item;
const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: 'transparent',
    },

});

const WheelTimePicker = ({selectedItems, receiveTime}) => {
    const _renderPikcer = () => {
       firstData = {
            secondList: [],
            minuteList: [],
        };
    for (var i = 0; i < 60; i++) {
            firstData.secondList.push(i + 'sec');
            firstData.minuteList.push(i + 'min');
        }

        return (
            <View> 
             <Picker style={{ left: -70, width: 60, height: 60 }}
                selectedValue={selectedItems.selectedMin}
                itemStyle={{ color: "white", fontSize: 20 }}
                onValueChange={(index) => { selectedItems.selectedMin = index;receiveTime(selectedItems); }}>
                {firstData.minuteList.map((value, i) => (
                    <PickerItem label={value} value={i} key={"money" + value} />
                ))}
            </Picker>
            <Picker style={{ left: 60, width: 60, height: 70 }}
                selectedValue={selectedItems.selectedSec}
                itemStyle={{ color: "white", fontSize: 20 }}
                onValueChange={(index) => { selectedItems.selectedSec = index; receiveTime(selectedItems) }}>
                {firstData.secondList.map((value, i) => (
                    <PickerItem label={value} value={i} key={"money" + value} />
                ))}
            </Picker>
            </View>
        );
   }

    return (

        <View style={styles.container}>
  {_renderPikcer()}

        </View>
    );

};

WheelTimePicker.propTypes = { 
     selectedItems: PropTypes.object.isRequired,
  receiveTime: PropTypes.func.isRequired,
};
export default WheelTimePicker;

I put the component like this: 我把这样的组件:

 <WheelTimePicker selectedItems={{selectedMin: 2, selectedSec: 0}} receiveTime={(rc)=> _displayTime(rc)}/>

This is because you are trying to modify props that is getting passed down from the parent element. 这是因为您正在尝试修改从父元素传递过来的props Ie in a stateless component, the parameter passed in is the props. 即在无状态组件中,传入的参数是props。 Props are read only ( https://facebook.github.io/react/docs/components-and-props.html#props-are-read-only ) 道具是只读的( https://facebook.github.io/react/docs/components-and-props.html#props-are-read-only

Also you are trying to update values inside render function that can potentially live in the state of the component. 另外,您正在尝试更新渲染函数中的值,这些值可能存在于组件状态中。 In this particular scenario, you are trying to modify the content of selectedItems which is a props that is getting passed down to your component. 在这种特定情况下,您尝试修改selectedItems的内容, selectedItems是一个传递到组件的道具。

In order to update the content of selectedItems , it would be better to keep selectedItems as the state in the parent component and use setState to update the values (this is probably your original approach, since you mentioned "It works well when cover it in the stateful component") 为了更新selectedItems的内容,最好将selectedItems保留为父组件中的状态,并使用setState来更新值(这可能是您的原始方法,因为您曾提到过“当在有状态的组件”)

For example 例如

class ParentComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      selectedItems: {
        selectedMin: 2,
        selectedSec: 0
      }
    }
  }

  _updateSelectedItems(rc) {
    this.setState({
      selectedItems: {
        selectedMin: rc.selectedMin || this.state.selectedMin,
        selectedSec: rc.selectedSec || this.state.selectedSec
      }
    })
  }

  render() {
    return (
      <WheelTimePicker selectedItems={this.state.selectedItems} receiveTime={(rc) => this._updateSelectedItems(rc)}/>
    )
  }
}

This way, your WheelTimePicker component can be stateless. 这样, WheelTimePicker组件可以是无状态的。 And in your Picker , probably would need to adjust it a little to something like: 在您的Picker ,可能需要对其进行一些调整,例如:

<Picker
  ...
  onValueChange={(index) => { receiveTime({selectedMin:index});}}
/>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM