File size: 3,444 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React from 'react';
import { useForm, Control, useWatch, Controller } from 'react-hook-form';
import { useRef } from 'react';

type FormInputs = {
  test: string;
  test1: string;
  test2: string;
};

let counter = 0;

const GrandChild = ({
  control,
  index = 0,
}: {
  control: Control<FormInputs>;
  index?: number;
}) => {
  const counter1 = useRef(0);
  const output = useWatch({
    name: 'test',
    control,
  });

  counter1.current++;

  return (
    <div style={{ border: '2px solid blue', padding: 10, margin: 5 }}>
      <h2 style={{ margin: 0 }}>Grandchild 0:</h2>
      <p id={`grandchild0${index}`}>{output}</p>
      <p id="grandChildCounter">Render counter: {counter1.current}</p>
    </div>
  );
};

const GrandChild1 = ({ control }: { control: Control<FormInputs> }) => {
  const counter = useRef(0);
  const output = useWatch<FormInputs>({
    name: ['test', 'test1'],
    control,
  });

  counter.current++;

  return (
    <div style={{ border: '2px solid blue', padding: 10, margin: 5 }}>
      <h2 style={{ margin: 0 }}>Grandchild 1:</h2>
      <p id="grandchild1">
        {output[0]}
        {output[1]}
      </p>
      <p id="grandChild1Counter">Render counter: {counter.current}</p>
    </div>
  );
};

const GrandChild2 = ({
  control,
}: {
  control: Control<{
    test: string;
    test1: string;
    test2: string;
  }>;
}) => {
  const counter = useRef(0);
  const output = useWatch<{
    test: string;
    test1: string;
    test2: string;
  }>({
    control,
  });

  counter.current++;

  return (
    <div style={{ border: '2px solid blue', padding: 10, margin: 5 }}>
      <h2 style={{ margin: 0 }}>Grandchild 2:</h2>
      <p id="grandchild2">
        {output.test}
        {output.test1}
        {output.test2}
      </p>
      <p id="grandChild2Counter">Render counter: {counter.current}</p>
    </div>
  );
};

const Child = ({ control }: { control: Control<FormInputs> }) => {
  const counter1 = useRef(0);
  counter1.current++;

  return (
    <div style={{ border: '2px solid green', padding: 10, margin: 5 }}>
      <h2 style={{ margin: 0 }}>Child:</h2>
      <GrandChild index={1} control={control} />
      <p id="childCounter" style={{ color: 'red' }}>
        <b>Render counter: {counter1.current} πŸ‘€</b>
      </p>
    </div>
  );
};

export default () => {
  const { register, control } = useForm<FormInputs>();

  counter++;

  return (
    <div style={{ border: '2px solid red', padding: 10, margin: 5 }}>
      <h2 style={{ margin: 0 }}>Parent:</h2>
      <input
        {...register('test')}
        autoComplete="off"
        placeholder="πŸ‘€ watching me :)"
        style={{ fontSize: 20 }}
      />

      <Controller
        name="test1"
        control={control}
        render={({ field }) => (
          <input
            placeholder="πŸ‘€ watching me :)"
            autoComplete="off"
            style={{ fontSize: 20 }}
            {...field}
          />
        )}
        defaultValue=""
      />

      <input
        {...register('test2')}
        name="test2"
        autoComplete="off"
        placeholder="πŸ‘€ watching me :)"
        style={{ fontSize: 20 }}
      />

      <GrandChild control={control} />
      <Child control={control} />
      <GrandChild1 control={control} />
      <GrandChild2 control={control} />

      <p id="parentCounter" style={{ color: 'red' }}>
        <b>Render counter: {counter} πŸ‘€</b>
      </p>
    </div>
  );
};