创建运行时视图,但无法使用setState更改和设置值

时间:2019-06-12 12:10:05

标签: react-native

在这里我想基于响应创建动态视图,并且已经创建视图,但是当我创建复选框时,我无法处理事件并且不能更改复选框的值

MarketSurvey类扩展了组件{

constructor(props) {
    super(props);
    this.state = {
        QuestionList: []
    }
}

componentWillMount() {
    try {
        let res = new clsGetQusAnsRes(jSon);
        this.setState({ QuestionList: res.tab });

    } catch (error) {
        console.log(error);
    }
}

render() {

    let views = [];

    for (let i = 0; i < this.state.QuestionList.length; i++) {

        if (this.state.QuestionList[i].QUESTIONCATEGORY == "SELECTIVE") {

            let answer = [];

            let option = this.state.QuestionList[i].ANSWER.toString().split(",");

            let alOption = [];

            for (let j = 0; j < option.length; j++) {
                let temp = {
                    id: option[j].split("#")[0],
                    name: option[j].split("#")[1],
                    isSelected: false,
                }
                alOption.push(temp);

            }

            this.state.QuestionList[i].reqOption = alOption;



            if (this.state.QuestionList[i].QUESTIONSUBCATEGORY == "ANY") {

                for (let j = 0; j < this.state.QuestionList[i].reqOption.length; j++) {
                    answer.push(
                        <View
                            style={{ flexDirection: "row" }}>
                            <Radio
                                selected={this.state.QuestionList[i].reqOption[j].isSelected}
                                onPress={() => {

                                    let tempdata = this.state.QuestionList;
                                    tempdata[i].reqOption[j].isSelected = !tempdata[i].reqOption[j].isSelected;
                                    this.setState({ QuestionList: tempdata });
                                }} />
                            <Text>
                                {this.state.QuestionList[i].reqOption[j].name}</Text>
                        </View >
                    );
                }
            } else if (this.state.QuestionList[i].QUESTIONSUBCATEGORY == "MANY") {

                for (let j = 0; j < this.state.QuestionList[i].reqOption.length; j++) {
                    answer.push(
                        <View style={{
                            flexDirection: 'row', margin: 5,
                            backgroundColor: this.state.QuestionList[i].reqOption[j].isSelected ? R.colors.colorWhite : R.colors.grey
                        }}>
                            <CheckBox
                                value={this.state.QuestionList[i].reqOption[j].isSelected}
                            />
                            {
                                this.state.QuestionList[i].reqOption[j].isSelected ? console.log("OK") : console.log("cancel")
                            }
                            <TextInput
                                onChangeText={(text) => console.log(text)} />
                            <Text style={{
                            }}
                                onPress={() => {
                                    let tempdata = this.state.QuestionList;
                                    tempdata[i].reqOption[j].isSelected = true;
                                    this.setState({ QuestionList: tempdata });
                                }}>
                                {this.state.QuestionList[i].reqOption[j].name}
                            </Text>
                        </View>
                    );
                }
            }

            views.push(
                <Card style={{ marginLeft: 5, marginRight: 5, marginTop: 5, marginBottom: 5 }}>

                    <Text style={{ margin: 10 }}> {"(" + (i + 1) + ") " +
                        this.state.QuestionList[i].QUESTION} </Text>

                    <View>
                        {answer}
                    </View>

                </Card>
            );
        }
    }


    return (
        <View style={{ flex: 1 }}>
            {views}

        </View>
    );
}

我在这里发布了我的代码,但是我无法在for循环中处理setState。请为这个问题指导我

0 个答案:

没有答案