This unit covers practical use-cases with components. Let's start playing with React :)
NOTICE: before you start working, please checkout branch novice-3 and use it to develop this unit tasks
- create file
.env
based on content of.env.template
, ask the keys from the trainer..- NOTICE everytime you change a value in
.env
file you must run againnpm run serve
.
- NOTICE everytime you change a value in
- in
app.tsx
- upon mounting- show
Loading...
message when mounting - connect to ChatKit using
./src/chatkit-service.ts
classChatkitService
methodconnect
- show
- if connection failed, replace loading with message
Failed to connect...
- if connected, replace loading message with
Hello {your name}!
Troubleshooting: If your IDE complains about unknown import of .css files, add declare module '*.css';
in file react-app-env.d.ts
Once completed, you can review the suggested solution here
You can continue your work or checkout and continue with branch novice-3.1
- add new component named
message-create
according coding convention:
src
|- components
|- message-create
|- message-create.tsx
|- message-create.css
|- index.ts
- in
MessageCreate
component render the following:
<>
<div className={'caption'}>Type your message here</div>
<input />
</>
- in
index.ts
addexport * from './message-create.tsx'
. - show
MessageCreate
component inapp.tsx
only if user is connected - use the
caption
class name inMessageCreate
to change the div style tofont-size: 12px; background: purple;
Once completed, you can review the suggested solution here
You can continue your work or checkout and continue with branch novice-3.2
- refactor two existing components to use css modules and make sure the conflict of
captions
is resolved. Use the article found in 'references' section. - feel free to remove those not that sofisticated class names, they did their part and not needed anymore.
- add state
value: string
toMessageCreate
. - add
onChange
react event to theinput
component. - add a method to handle that event, when called it should update the value of the state. Use the article as a reference.
- tip: any method used to handle events should be created as arrow functions otherwise you might have issues with
this
. instead ofhandleChange() {}
dohandleChange = () => {}
.
- tip: any method used to handle events should be created as arrow functions otherwise you might have issues with
- add second event
onKeyUp
and a matching method to handle it. use event argse.which === 13
to detect pressingenter
and write the value to console.
Once completed, you can review the suggested solution here
You can continue your work or checkout and continue with branch novice-3.3
- move
value
fromMessageCreate
state intoApp
state - extend
MessageCreate
props, addonChange
andonSend
events.onChange
is a 'proxy event' and should have the same signature of the originator. In our case(e: any) => void
.onSend
abstract the logic ofonKeyUp
and can expose a more relevant signature() => void
- adjust code so a user can type value and print it to console when pressing enter. It should behave the same while the value should be managed in
App
component. - instead of writing to console use
ChatkitService
methodsendMessage
to send the message to the server. Ask for the roomId from the trainer and add that value to.env
file with keyREACT_APP_CHATKIT_TEST_ROOM_ID
- NOTICE everytime you change a value in
.env
file you must run againnpm run serve
.
- NOTICE everytime you change a value in
- expose optional
disabled
prop inMessageCreate
that if set disable user typing. default to not disabled. - add 'swear filter' that replaces any of the following words ('badass', 'sexy', 'motherfucker') with 'nice guy'
- add 'antisocial filter' that disable the input if the user types the phrase 'coffee break' useful link
Once completed, you can review the suggested solution here
You can continue your work or checkout and continue with branch novice-3.4
This ticket provides less guidance to level up the challange. Ping me if you need assistance
- continuing from previous task, once the input field is disabled, add event to
MessageCreate
that will be triggered after 5 seconds and will causeApp
to re-enable the component. - when user types 'i need your help', make
MessageCreate
to write to console 'Sorry, I'm very busy...' every 1 second eternally. useful link - when user types 'lo tzarich tovot', make
App
to disconnect (which will causeMessageCreate
to be unmounted). - make sure the writes to console log stops as
MessageCreate
is not there anymore.
Once completed, you can review the suggested solution here
You can continue your work or checkout and continue with branch novice-3.5
- extends
App
state with
bookmarks: {
list: {id: string, value: string}[],
ownerIdType: string
}
- upon mounting - set value of ownerIdType to string
User
- when user types
show bookmarks
- print all bookmarks to console with titleshowing bookmarks for '${this.state.ownerIdType}'
. - when user types
add bookmark {something somthing}!
- add new bookmark. generate id using npm librarishort
library. don't forget to add types as well by consuming@types/shortid
(into devDependencies) - make sure adding to array doesn't delete value of
ownerId
without re-setting is manually. Search for the functional programming technique without adding 3rd party libraries - when user types
remove bookmark XXX!
- remove bookmark with id XXX if exists and print to console new bookmarks - when use types
modify bookmark {id} {new value}!
- modify value if id exists
Once completed, you can review the suggested solution here
- component state
- component typescript support for props & state
- workaround for state issue with typescript
- using .env local file
- workaround libraries without typescript declarations
- css resets
- fragments
- css modules
- controlled components
- react synthetic events
- default props
- component life cycle events (componentDidMount, componentWillUnmount, componentDidUpdate)