簡體   English   中英

如何更新JSX元素的狀態存在於狀態?

[英]How to update state for JSX element exist in state?

我在狀態中有一個JSX元素和一個計數器,JSX元素在狀態中使用計數器狀態。 我使用modal在組件1中顯示JSX元素,並在component2中設置JSX元素。

當我嘗試更新組件2中的計數器時,它將不會在組件1中的JSX元素計數器中更改。

組件1

class Meeting extends Component {

    render() {
        const { dispatch, modalVisible, modalContent } = this.props;

        return (
            <Landing>
                <Modal
                    title="Title"
                    visible={modalVisible}
                    onOk={() => { dispatch(showModal(false)); }}
                    onCancel={() => { dispatch(showModal(false)); }}
                    okText="Ok"
                    cancelText="Cancel"
                >
                    <div>
                        {modalContent}
                    </div>
                </Modal>
            </Landing>
        );
    }
}

function mapStateToProps(state) {
    const {
        modalVisible,
        modalContent,
        counter
    } = state.meeting;

    return {
        modalVisible,
        modalContent,
        counter
    };
}

export default connect(mapStateToProps)(Meeting);

第2部分

class MeetingItem extends Component {

    state = {
        checked: []
    }

    handleChange = (event) => {
        const { dispatch, counter } = this.props;
        if (event.target.checked) {
            this.setState(() => {
                return { checked: [...this.state.checked, event.target.value] };
            });
            dispatch(setCounter(counter - 1));
        } else {
            const array = this.state.checked;
            const index = array.indexOf(event.target.value);
            if (index > -1) {
                array.splice(index, 1);
                this.setState(() => {
                    return { checked: array };
                });
                dispatch(setCounter(counter + 1));
            }

        }

    };

    isDisable = (val) => {
        const array = this.state.checked;
        const index = array.indexOf(val);
        if (index > -1) {
            return true;
        } else {
            return false;
        }
    }

    showModal = () => {
        const { dispatch, question, counter } = this.props;

        const radioStyle = {
            display: 'block',
            height: '30px',
            lineHeight: '30px',
            marginTop: '6px'
        };

        dispatch(setCounter(0));

        switch (question.question.TypeAnswer) {
            case 'OneAnswer':
                const answers = question.answer.map((record, i) => {
                    return <Radio.Button style={radioStyle} key={i} value={record.Id}>{record.Title}</Radio.Button>
                });
                const modalContent = <div>
                    <p>{question.question.Title}</p>
                    <Radio.Group buttonStyle="solid">
                        {answers}
                    </Radio.Group>
                </div>

                dispatch(setModalContent(modalContent));
                break;
            case 'MultiAnswer':
                dispatch(setCounter(question.question.CountAnswer));

                const multiAnswers = question.answer.map((record, i) => {
                    return <Checkbox key={i} value={record.Id} onChange={this.handleChange}>{record.Title}</Checkbox>
                });
                const multiModalContent = <div>
                    <p>{question.question.Title}</p>
                    <p>counter: {counter}</p>
                    <Checkbox.Group>
                        {multiAnswers}
                    </Checkbox.Group>
                </div>

                dispatch(setModalContent(multiModalContent));
                break;
            case 'PercentAnswer':

                break;
            default: <div></div>
                break;
        }

        dispatch(showModal(true));
    };

    render() {
        const { title, question, fileDoc } = this.props;

        return (
            <Timeline.Item className='ant-timeline-item-right'>
                <span style={{ fontSize: '16px', fontWeight: 'bolder' }}>{title}</span> <span className='textAction' onClick={this.showModal}>showModal</span>
                {/* <br /> */}
                {/* <span>12:00</span>  <Icon type="clock-circle" /> */}
            </Timeline.Item>
        );
    }
}

function mapStateToProps(state) {
    const {
        visibleModal,
        counter
    } = state.meeting;

    return {
        visibleModal,
        counter
    };
}

export default connect(mapStateToProps)(MeetingItem);

行動

export const setCounter = (counter) => (dispatch) => {
    return dispatch({ type: actionTypes.SET_COUNTER, counter })
}

您必須將mapDispatchToProps用於connect函數。

...
function mapDispatchToProps (dispatch) {
    propAction: (action) => dispatch(reduxAction(action))
}
export default connect(
  mapStateToProps,
  mapDispatchToProps
)(MeetingItem);
...

請遵循本教程

只需在連接中使用mapDispatchToProps。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM