在Redux中清除store中的输入值可以通过以下步骤实现:
const clearInput = () => {
return {
type: 'CLEAR_INPUT'
};
};
const initialState = {
input: ''
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case 'CLEAR_INPUT':
return {
...state,
input: ''
};
default:
return state;
}
};
import { connect } from 'react-redux';
import { clearInput } from './actions';
const MyComponent = ({ input, clearInput }) => {
return (
<div>
<input type="text" value={input} />
<button onClick={clearInput}>Clear Input</button>
</div>
);
};
const mapStateToProps = state => {
return {
input: state.input
};
};
const mapDispatchToProps = {
clearInput
};
export default connect(mapStateToProps, mapDispatchToProps)(MyComponent);
这样,当点击"Clear Input"按钮时,Redux store中的输入值将被清除。
领取专属 10元无门槛券
手把手带您无忧上云