1
當我單擊按鈕時,我總是會收到警告。我搜索它,他們使用綁定來移除警告,但我怎麼可以添加它來反應在我的代碼綁定功能的redux?警告:setState(...):無法在反應過程中的現有狀態轉換期間更新
import * as React from 'react';
import {connect} from 'react-redux';
import {toggleModal} from '../../actions/ModalActions';
interface Props {
show?: boolean;
onSelectedSample: (value: boolean) => void;
}
const SampleList = ({show, onSelectedSample}: Props) => {
return (
<div>
<a className='button link info' onClick={() => onSelectedSample(!show)}><i className='fa fa-user-plus fa-fw'></i></a>
<a className='button link info' onClick={() => onSelectedSample(!show)}><i className='fa fa-user-plus fa-fw'></i></a>
<a className='button link info' onClick={() => onSelectedSample(!show)}><i className='fa fa-user-plus fa-fw'></i></a>
</div>
);
});
const mapStateToProps = (state, props) => {
return {
show: state.modalReducer.show,
};
};
const mapDispatchToProps = (dispatch, props) => {
return {
onSelectedSample: (toggle: boolean){
dispatch(toggleModal(toggle));
},
};
};
const ListSampleContainer = connect(mapStateToProps, mapDispatchToProps)(SampleList);
export default ListSampleContainer;
在redux代碼中很難。我不知道如何在redux中處理這個問題。
更新
Actions文件夾
import {ModalActions} from '../constants/ModalConstants';
export const toggleModal = (show?: boolean) => {
return {
type: ModalActions.TOGGLE_MODAL,
show,
};
};
減速夾
import {ModalActions} from '../constants/ModalConstants';
interface State {
show: boolean;
}
const initState = {show: false};
export const modalReducer = (state: State = initState, action) => {
switch (action.type) {
case ModalActions.TOGGLE_MODAL:
if (action.show) {
return {show: action.show};
} else {
return {show: !state.show};
}
case ModalActions.HIDE_MODAL:
return {show: false};
}
return state;
};
如果我只有一個按鈕。我沒有警告,但添加了更多按鈕。那將會發生
您使用了關鍵字'interface' ...它不是真的js嗎? –
.tsx(打字稿) – user3818576
減速機中的toggleModal的結果是什麼? –