I am trying to create a React App which should have two screens:
In the First Screen, the user can enter some input as a string and upon submitting it, the string should be passed on to Screen2
In Screen2
, the entered text should be split and displayed as individual letters on a react bootstrap card.
The code which I have written for Screen1
is working perfectly and passing the string as intended to Screen2
. However, the Screen2
is not displaying anything.
Code for App.js
export default function App() {
return (
<Router>
<div className="App">
<Nav />
<Routes>
<Route path="/" element={<Home />} />
<Route path="/about" element={<About />} />
<Route exact path="/Screen1" element={<Screen1 />} />
<Route path="/Screen2/:text" component={Screen2} />
</Routes>
</div>
</Router>
)
}
This the code for Screen1.js
import React, { useState } from 'react'
import { Link } from 'react-router-dom'
export default function Screen1() {
const [text, setText] = useState('')
const [errorMsg, setErrorMsg] = useState('')
const handleTextChange = (e) => {
setText(e.target.value)
}
const handleSubmit = (e) => {
e.preventDefault()
if (text.trim() === '') {
setErrorMsg('Please enter some text.')
return
}
setText('')
}
return (
<div className="info">
<h2>Screen 1</h2>
<form onSubmit={handleSubmit}>
<label>
Enter text:
<input
type="text"
value={text}
onChange={handleTextChange}
/>
</label>
<Link to={`/Screen2/${text}`}>
<button type="submit">Submit</button>
</Link>
{errorMsg && <p className="error">{errorMsg}</p>}
</form>
<Link to="/">Go back to home</Link>
</div>
)
}
This is the code for Screen2.js
import React from 'react'
import { useParams } from 'react-router-dom'
import Card from 'react-bootstrap/Card'
function Screen2() {
const { text } = useParams() // extract the text from the URL
const letters = text.split('') // convert text to array of characters
return (
<div className="info">
<h2>Screen 2</h2>
<Card style={{ width: '18rem' }}>
<Card.Body>
{letters.map((letter, index) => (
<Card.Text key={index}>{letter}</Card.Text> // render each letter
))}
</Card.Body>
</Card>
</div>
)
}
export default Screen2
I tried making the card from react-bootstrap-documentation
Upon, inspecting and checking the console in Screen2, I got this error message:
Matched leaf route at location "/Screen2/eavdsdvsfs" does not have an element or Component. This means it will render an
<Outlet />
with a null value by default resulting in an "empty" page.