Skip to content

Commit ba04a04

Browse files
committed
fix(react-login-page): fix control render issue.
1 parent 64a0f8b commit ba04a04

File tree

6 files changed

+35
-33
lines changed

6 files changed

+35
-33
lines changed

core/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,13 @@ import React from 'react';
232232
import Login, { Render } from 'react-login-page';
233233

234234
const Demo = () => {
235+
const [name, setName] = React.useState(1);
236+
console.log('name:', name);
235237
return (
236238
<Login>
237239
<Render>{({ blocks, fields, $$index, extra }, data) => <label>{blocks.title}</label>}</Render>
238-
<Login.Block keyname="title">Login</Login.Block>
240+
<Login.Block keyname="title">{name} Login</Login.Block>
241+
<button onClick={() => setName(name + 1)}>++</button>
239242
</Login>
240243
);
241244
};

core/src/Block.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PropsWithChildren, useRef, useEffect, createElement, AllHTMLAttributes } from 'react';
1+
import { PropsWithChildren, useEffect, createElement, AllHTMLAttributes, memo } from 'react';
22
import { useStore, BlockTagType, Blocks } from './store';
33

44
export interface BlockProps<T extends BlockTagType> extends AllHTMLAttributes<T> {
@@ -15,22 +15,21 @@ export interface BlockProps<T extends BlockTagType> extends AllHTMLAttributes<T>
1515
tagName?: T;
1616
}
1717

18-
export const Block = <Tag extends BlockTagType = 'div'>(props: PropsWithChildren<Partial<BlockProps<Tag>>>) => {
19-
const ref = useRef<Partial<BlockProps<Tag>>>();
18+
export const Block = memo(<Tag extends BlockTagType = 'div'>(props: PropsWithChildren<Partial<BlockProps<Tag>>>) => {
2019
const { blocks = {}, dispatch } = useStore();
20+
const { name, keyname, visible = true, tagName = 'div', ...elmProps } = props;
2121
useEffect(() => {
22-
const { name, keyname, visible = true, tagName = 'div', ...elmProps } = props;
23-
if (ref.current !== elmProps && (name || keyname)) {
22+
if (name || keyname) {
2423
const key = (keyname || name) as string;
25-
ref.current = { ...elmProps };
2624
const div = visible ? createElement(tagName, { ...elmProps }, elmProps.children) : null;
25+
delete blocks[key];
2726
dispatch({
2827
blocks: { ...blocks, [key]: div as unknown as Blocks<'div'> },
2928
});
3029
}
31-
}, [props, ref]);
30+
}, [props]);
3231

3332
return null;
34-
};
33+
});
3534

3635
Block.displayName = 'Login.Block';

core/src/Button.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { FC, PropsWithChildren, useRef, useEffect } from 'react';
1+
import { FC, PropsWithChildren, useEffect, memo } from 'react';
22
import { useStore } from './store';
33

44
export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElement> {
@@ -9,21 +9,20 @@ export interface ButtonProps extends React.ButtonHTMLAttributes<HTMLButtonElemen
99
index?: number;
1010
}
1111

12-
export const Button: FC<PropsWithChildren<ButtonProps>> = (props) => {
13-
const ref = useRef<ButtonProps>();
12+
export const Button: FC<PropsWithChildren<ButtonProps>> = memo((props) => {
1413
const { buttons = {}, dispatch } = useStore();
1514
useEffect(() => {
1615
const { keyname, visible = true, ...elmProps } = props;
17-
if (ref.current !== elmProps && (keyname || elmProps.name)) {
18-
ref.current = { ...elmProps };
16+
if (keyname || elmProps.name) {
1917
const key = (keyname || elmProps.name) as string;
18+
delete buttons[key];
2019
dispatch({
2120
buttons: { ...buttons, [key]: visible ? <button type="submit" {...elmProps} /> : null },
2221
});
2322
}
24-
}, [props, ref]);
23+
}, [props]);
2524

2625
return null;
27-
};
26+
});
2827

2928
Button.displayName = 'Login.Button';

core/src/Input.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, PropsWithChildren, useRef, useEffect, memo } from 'react';
1+
import React, { FC, PropsWithChildren, useEffect, memo } from 'react';
22
import { useStore } from './store';
33

44
export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
@@ -15,13 +15,14 @@ export interface InputProps extends React.InputHTMLAttributes<HTMLInputElement>
1515
}
1616

1717
export const Input: FC<PropsWithChildren<InputProps>> = memo((props) => {
18-
const ref = useRef<InputProps>();
1918
const { fields = {}, extra = {}, $$index = {}, dispatch } = useStore();
2019
const { rename, keyname, visible = true, children, ...elmProps } = props;
2120
useEffect(() => {
22-
if (ref.current !== props && (keyname || elmProps.name)) {
21+
if (keyname || elmProps.name) {
2322
const key = (keyname || elmProps.name) as string;
24-
ref.current = { ...props };
23+
delete $$index[key];
24+
delete fields[key];
25+
delete extra[key];
2526
dispatch({
2627
$$index: { ...$$index, [key]: elmProps.index || 0 },
2728
extra: {
@@ -34,7 +35,7 @@ export const Input: FC<PropsWithChildren<InputProps>> = memo((props) => {
3435
},
3536
});
3637
}
37-
}, [props, ref]);
38+
}, [props]);
3839

3940
return null;
4041
});

core/src/Select.tsx

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, PropsWithChildren, useRef, useEffect } from 'react';
1+
import React, { FC, PropsWithChildren, useEffect, memo } from 'react';
22
import { useStore } from './store';
33

44
export interface SelectProps extends React.InputHTMLAttributes<HTMLSelectElement> {
@@ -14,21 +14,20 @@ export interface SelectProps extends React.InputHTMLAttributes<HTMLSelectElement
1414
index?: number;
1515
}
1616

17-
export const Select: FC<PropsWithChildren<SelectProps>> = (props) => {
18-
const ref = useRef<SelectProps>();
17+
export const Select: FC<PropsWithChildren<SelectProps>> = memo((props) => {
1918
const { fields = {}, dispatch } = useStore();
2019
const { rename, keyname, visible = true, ...elmProps } = props;
2120
useEffect(() => {
22-
if (ref.current !== props && (keyname || props.name)) {
21+
if (keyname || props.name) {
2322
const key = (keyname || props.name) as string;
24-
ref.current = { ...props };
23+
delete fields[key];
2524
dispatch({
2625
fields: { ...fields, [key]: visible ? <select {...elmProps} name={rename || props.name} /> : null },
2726
});
2827
}
29-
}, [props, ref]);
28+
}, [props]);
3029

3130
return null;
32-
};
31+
});
3332

3433
Select.displayName = 'Login.Select';

core/src/Textarea.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { FC, memo, PropsWithChildren, useRef, useEffect } from 'react';
1+
import React, { FC, memo, PropsWithChildren, useEffect } from 'react';
22
import { useStore } from './store';
33

44
export interface TextareaProps extends React.InputHTMLAttributes<HTMLTextAreaElement> {
@@ -15,13 +15,14 @@ export interface TextareaProps extends React.InputHTMLAttributes<HTMLTextAreaEle
1515
}
1616

1717
export const Textarea: FC<PropsWithChildren<TextareaProps>> = memo((props) => {
18-
const ref = useRef<TextareaProps>();
1918
const { fields = {}, extra = {}, $$index = {}, dispatch } = useStore();
2019
useEffect(() => {
2120
const { rename, keyname, visible = true, children, ...elmProps } = props;
22-
if (ref.current !== props && (keyname || elmProps.name)) {
21+
if (keyname || elmProps.name) {
2322
const key = (keyname || elmProps.name) as string;
24-
ref.current = { ...props };
23+
delete fields[key];
24+
delete extra[key];
25+
delete $$index[key];
2526
dispatch({
2627
$$index: { ...$$index, [key]: elmProps.index || 0 },
2728
extra: {
@@ -31,7 +32,7 @@ export const Textarea: FC<PropsWithChildren<TextareaProps>> = memo((props) => {
3132
fields: { ...fields, [key]: visible ? <textarea {...elmProps} name={rename || elmProps.name} /> : null },
3233
});
3334
}
34-
}, [props, ref]);
35+
}, [props]);
3536

3637
return null;
3738
});

0 commit comments

Comments
 (0)