I need to understand the propose of using window.location?.search?.split('=')[1]
and why id takes value of window.location?.search?.split('=')[1]
Code:
function EndScreen() {
const [score, setScore] = React.useContext(ScoreContext);
/* console.log('history', window.location?.search?.split('=')[1]); */
return (
<div className="EndScreen">
<h1>Test finished</h1>
<br></br>
<h3> You scored:</h3>
<h1 style={{ fontSize: '70px' }}>
{score} / {Questions?.length}
</h1>
<br></br>
<br></br>
<button
onClick={() =>
FormService.update({
id: window.location?.search?.split('=')[1],
data: { score: score }
})
.then(() => {
alert('Score updated');
})
.catch(() => {
alert('Score did NOT update')
})
}
>
Apply for job
</button>
</div>
);
}
Its for working with querystring:
if your url is , google.com/user/userData?id=123,email=exp@gmail.com
we call user and userData path & id and email are called querystrings
location.search; // Returns:'?id=123,email=exp@gmail.com'
let newArray = location.search.split('=') // Returns:['?id','123',',email','exp@gmail.com']