我需要在聊天应用程序中进行状态管理方面的帮助。
Description:
I am attempting to display the username entered from the UserModal
component to my ChatScreen
component. I am using the onChange
function inside UserModal
and I am using a switch case
statement to ensure form validation. Then setting the state with name as an array and assigning the value to the name like so:
UserModal.js
onChange = e => {
e.preventDefault();
const { name, value } = e.target;
let formError = this.state.formError;
switch (name) {
case 'userName':
formError.userName =
value.length < 3 ? 'minimum 3 characters required' : '';
break;
default:
}
this.setState({ formError, [name]: value }, () =>
console.log(this.state)
);
};
Goal:
My goal is to take what the user puts in the username field in the modal and display "{userName} has joined the chat." where userName
is replaced with the input of the user, in the ChatScreen.js component within the Semantic UI fragments.
Github Link: https://github.com/George-Jindo/chat-app