My project use dvajs(Based on redux and redux-saga), The code below is to send a request after clicking the button, change the status through connect, and then call the ant design component message.error an message.success(Similar to alert) to remind
import type { Dispatch } from 'umi';
import ProForm, { ProFormText } from '@ant-design/pro-form';
import { message } from 'antd';
const tip = (type: string, content: string) => {
if (type === 'error') message.error(content, 5);
else message.success(content, 5);
};
const RegisterFC: React.FC<RegisterProps> = (props) => {
const { registerResponseInfo = {}, submitting, dispatch } = props;
const { status } = registerResponseInfo;
const handleSubmit = (values: RegisterParamsType) => {
dispatch({
type: 'register/register',
payload: { ...values },
});
};
return (
<div>
<ProForm
onFinish={(values) => {
handleSubmit(values as RegisterParamsType);
return Promise.resolve();
}}
>
<ProFormText/>
...
{
status === '1' && !submitting && (
tip('error',
intl.formatMessage({
id: 'pages.register.status1.message',
defaultMessage: 'error'
})
)
)
}
<<ProForm>/>
</div>
)
}
const p = ({ register, loading }: { register: RegisterResponseInfo, loading: Loading; }) => {
console.log(loading);
return {
registerResponseInfo: register,
submitting: loading.effects['register/register'],
};
};
export default connect(p)(RegisterFC);
When I click the button, the console prompts:
Warning: Render methods should be a pure function of props and state; triggering nested component updates from render is not allowed. If necessary, trigger nested updates in componentDidUpdate.
Doesn't the component re-render when the state changes? Does the tip function change the state?
Solution: Call
tipOutside ofreturntipis just afunctionthat you are calling. You should call it outside of thereturnJSX section of your code. I think it makes the most sense to call it inside of auseEffecthook with dependencies onstatusandsubmitting. The effect runs each time thatstatusorsubmittingchanges. Ifstatusis1andsubmittingis falsy, then we calltip.Explanation
The render section of a component (
render()in class component orreturnin a function component) is where you create the JSX (React HTML) markup for your component based on the current values ofpropsandstate. It should not have any side effects. It creates and returns JSX and that's it.Calling
tipis a side effect since it modifies the globalantdmesssageobject. That means it shouldn't be in therendersection of the code. Side effects are generally handled inside ofuseEffecthooks.You are trying to conditionally render
tiplike you would conditionally render a component. The problem is thattipis not a component. A function component is a function which returns a JSX Element.tipis avoidfunction that returns nothing, so you cannot render it.