Skip to content

Commit 2884ea3

Browse files
committed
fixed value not changing on room input box due to defaultValue property, which I have replaced with value property instead
1 parent bebb85d commit 2884ea3

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"scripts": {
2525
"start": "react-scripts start",
2626
"build": "react-scripts build",
27-
"test": "react-scripts test --coverage",
27+
"test": "react-scripts test --watchAll=false --coverage",
2828
"eject": "react-scripts eject"
2929
},
3030
"eslintConfig": {

src/components/Join/Join.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const Join = ({ location }) => {
2626
`Ensure username and/or room name is clean`,
2727
]);
2828
const [error, setError] = useState(obj.error);
29-
const [defaultRoom, setDefaultRoom] = useState("");
3029
const [avatar, setAvatar] = useState("");
3130
const [index, setIndex] = useState(Math.floor(Math.random() * emojiList.length));
3231

@@ -35,7 +34,6 @@ const Join = ({ location }) => {
3534
// if queries present in url, retrieve them to display
3635
const { error } = queryString.parse(location.search);
3736
const { room } = queryString.parse(location.search);
38-
setDefaultRoom(room);
3937
setRoom(room);
4038
const newErrorList = [
4139
`Username is taken in room ${room}`,
@@ -51,7 +49,6 @@ const Join = ({ location }) => {
5149
// A function to get a random room name
5250
async function fetchData() {
5351
const response = await axios.get(process.env.REACT_APP_SERVER + "/room");
54-
setDefaultRoom(response.data.room);
5552
setRoom(response.data.room);
5653
const newErrorList = [
5754
`Username is taken in room ${response.data.room}`,
@@ -68,7 +65,7 @@ const Join = ({ location }) => {
6865
}
6966
return;
7067
// eslint-disable-next-line
71-
}, [error, location.search]);
68+
}, [location.search]);
7269

7370
// Remove active from carousel and randomise the index
7471
const handleSetIndex = () => {
@@ -89,6 +86,12 @@ const Join = ({ location }) => {
8986
pickEmoji(emojiList[index]);
9087
}, [index]);
9188

89+
useEffect(() => {
90+
if (name && room && error === "Username and/or room name is empty") {
91+
setError("");
92+
}
93+
}, [room, name, error]);
94+
9295
// Function to get the active emoji and set the avatar
9396
// Added a time out to allow the carousal to refresh and add active attribute
9497
const getKey = () => {
@@ -155,6 +158,7 @@ const Join = ({ location }) => {
155158
placeholder="Username"
156159
title="Type in a name that will be visible to others. Max length is 12 characters :)"
157160
maxLength="12"
161+
value={name}
158162
required
159163
onChange={(event) => setName(event.target.value.trim().toLowerCase())}
160164
/>
@@ -167,11 +171,13 @@ const Join = ({ location }) => {
167171
id="id_room"
168172
className="form-control"
169173
placeholder="Room"
170-
defaultValue={defaultRoom}
174+
value={room}
171175
title="Type in a room name. Could be one that is already created and you are joining or a brand new room. Max length is 150 characters"
172176
maxLength="150"
173177
required
174-
onChange={(event) => setRoom(event.target.value.trim().toLowerCase())}
178+
onChange={(event) => {
179+
setRoom(event.target.value.trim().toLowerCase());
180+
}}
175181
/>
176182
<label htmlFor="id_room">Room:</label>
177183
</div>

0 commit comments

Comments
 (0)