如何使用Recompose和withState
重新渲染React组件?我不了解如何更新子组件中的数据以随时随地进行更新。这是我的子组件,仅提交了表单。
import React, {Fragment} from 'react'
import PropTypes from 'prop-types';
import {compose, setPropTypes, withHandlers, withState, lifecycle, shouldUpdate} from 'recompose';
const enhance = compose(
setPropTypes({handleCheater: PropTypes.func, id: PropTypes.number,}),
withState('task', 'setCheater', []),
withHandlers({
handleCheater: props => () => {
axios.post(window.location.href + 'task/' + props.id, {id: props.id})
.then(response => {
props.setCheater([...response.data]);
}).catch(error => console.log(error))
}
})
);
const Cheater = ({handleCheater, id}) => (
<Fragment>
<form onSubmit={event => {
event.preventDefault();
handleCheater(id);
}}>
<button>Cheater</button>
</form>
</Fragment>
);
export default enhance(Cheater);