Blank white screen on localhost:3000 in my react project - reactjs

I am getting blank white screen on localhost:3000 in my React project. The components dont get rendered. I guess the issue is with one component, because when i comment out that component(SearchForm.js) from JSX, the other components do show up. Kindly highlight what could be the possiblities causing the issue. Thanks
SearchForm.js
import { Form, Col } from 'react-bootstrap'
export default function SearchForm({ params, onParamChange }) {
return (
<Form className="mb-4">
<Form.Row className="align-items-end">
<Form.Group as={Col}>
<Form.Label>Description</Form.Label>
<Form.Control onChange={onParamChange} value={params.description} name="description" type="text" />
</Form.Group>
<Form.Group as={Col}>
<Form.Label>Location</Form.Label>
<Form.Control onChange={onParamChange} value={params.location} name="location" type="text" />
</Form.Group>
<Form.Group as={Col} xs="auto" className="ml-2">
<Form.Check onChange={onParamChange} value={params.full_time} name="full_time" id="full-time" label="Only Full Time" type="checkbox" className="mb-2" />
</Form.Group>
</Form.Row>
</Form>
)
}
App.js
import React, { useState } from 'react';
import useFetchJobs from './useFetchJobs'
import { Container } from 'react-bootstrap'
import Job from './Job'
import JobsPagination from './JobsPagination';
import SearchForm from './SearchForm';
function App() {
const [params, setParams] = useState({})
const [page, setPage] = useState(1)
const { jobs, loading, error, hasNextPage } = useFetchJobs(params, page)
function handleParamChange(e) {
const param = e.target.name
const value = e.target.value
setPage(1)
setParams(prevParams => {
return { ...prevParams, [param]: value }
})
}
return (
<Container className="my-4">
<h1 className="mb-4">GitHub Jobs</h1>
<SearchForm params={params} onParamChange={handleParamChange} />
<JobsPagination page={page} setPage={setPage} hasNextPage={hasNextPage} />
{loading && <h1>Loading...</h1>}
{error && <h1>Error. Try Refreshing.</h1>}
{jobs.map(job => {
return <Job key={job.id} job={job} />
})}
<JobsPagination page={page} setPage={setPage} hasNextPage={hasNextPage} />
</Container>
)
}
export default App;

This seems okay, will need more information, Like how did you import and use it in another file.

After trying it out, I decided to just import { Row } directly from react-bootstrap.
But if you would like to continue with Form.Row, try checking how you imported bootstrap in index.js
import "bootstrap/dist/css/bootstrap.css";

Related

code renders an <Outlet /> with a null value by default resulting in an "empty" page

below is my code
import React, { useState } from "react";
import { Button, Form } from "react-bootstrap";
import { toast } from "react-toastify";
import { db } from "../firebase";
const PostForm = (props) => {
const [title, setTitle] = useState("");
const [person, setPerson] = useState("");
const [description, setDescription] = useState("");
const[loader, setLoader] =- useState(false)
const handleSubmit =(e)=>{
e.preventDefault()
setLoader(true)
db.collection('posts').add({
title:title,
person:person,
description:description,
})
.then(() =>{
toast.success("Activity Registered SuccessFully")
setLoader(false)
})
.catch((error) =>{
toast.error('Activty not registered')
setLoader(false)
})
setTitle("")
setPerson("")
setDescription("")
}
return (
<div>
<Form onSubmit={handleSubmit} >
<Form.Group className="mb-3" controlId="formBasicEmail">
<Form.Label>Title</Form.Label>
<Form.Control
type="text"
placeholder="Enter Title"
value={title}
onChange={(e) => setTitle(e.target.value)}
/>
<Form.Text className="text-muted">
Please include title of activity
</Form.Text>
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicPassword">
<Form.Label>Person Responsible</Form.Label>
<Form.Control
type="text"
placeholder="Author/ Person Responsible"
value={person}
onChange={(e) => setPerson(e.target.value)}
/>
</Form.Group>
<Form.Group className="mb-3" controlId="formBasicCheckbox">
<Form.Control
as="textarea"
placeholder="enter Activity Description"
style={{ height: "100px" }}
value={description}
onChange={(e) => setDescription(e.target.value)}
/>
</Form.Group>
<Button variant="primary" type="submit" style={{background:loader?"#ccc":"rgb(2,2,110)"}}>
Submit
</Button>
</Form>
</div>
);
};
export default PostForm;
App component
import PostForm from "./components/PostForm";
function App() {
return (
<div className="app">
<Router>
<Routes>
<Route exact path="/postreg" elememt={<PostForm />} />
</Routes>
</Router>
</div>
);
}
export default App;
Link tag
<Link to={"/postreg"}>Post</Link>
The above code brings the error as stated below
router.ts:11 Matched leaf route at location "/postreg" does not have an element. This means it will render an with a null value by default resulting in an "empty" page.
Am using react V18, Can someone please Help me understand where the problem is coming from
//In V6, you can't use the component prop anymore. It was replaced in favor of element:
<Route path="/" element={}>

Input value is showing undefined (reading 'value') while using rsuite library

It is basically rsuite#4.10.2 version
const [user, setuser] = useState('')
<InputGroup>
<Input type='text' value={user} onChange={e=>{setuser(e.target.value)}} id="user"/>
<InputGroup.Button>
<Icon icon="user" />
</InputGroup.Button>
</InputGroup>
also i have provided with the error image just look this out
enter image description here
rsuite library return the value directly in the onChange function.
So, instead:
onChange={e=>{setuser(e.target.value)}}
You should write
onChange={value=>{setuser(value)}}
This is a class that I have written with rsuite / bootstrap and basic input. You can see the difference and copy/paste the part that you want.
import React, { useState } from 'react';
import { InputGroup, Input } from 'rsuite';
import InputGroupBoostrap from 'react-bootstrap/InputGroup';
import FormControlBoostrap from 'react-bootstrap/FormControl';
const Question2 = () => {
const [user, setUser] = useState('example');
return (
<>
{/* rsuite */}
<InputGroupBoostrap>
<Input type='text' value={user} onChange={value=>{ setUser(value)}} id="user"/>
<InputGroup.Button>
Button
</InputGroup.Button>
{/* Bootstrap */}
</InputGroupBoostrap>
<InputGroupBoostrap>
<InputGroupBoostrap.Text>User 1</InputGroupBoostrap.Text>
<FormControlBoostrap value={user} onChange={(e) => { setUser(e.target.value); }} placeholder="user" />
</InputGroupBoostrap>
{/* input */}
<div>
<span>User 2</span>
<input value={user} onChange={(e) => { setUser(e.target.value); }}/>
</div>
</>
);
}
export default Question2;
I hope I've helped

Error: Too many re-renders. React limits the number of renders to prevent an infinite loop when using useState()

App.js:
import React, { useState } from "react";
import Login from "./Login";
function App() {
const [id, setId] = useState();
return (
<>
{id}
<Login onSubmit={setId()}/>
</>
);
}
export default App;
and
Login.js:
import React, { useRef } from 'react'
import { Container, Form, Button } from "react-bootstrap";
export default function Login({ onIdSubmit }) {
const idRef = useRef();
function handleSubmit(e) {
e.preventDefault()
onIdSubmit(idRef.current.value)
}
return (
<Container className="align-items-center d-flex" style={{height: "100vh"}}>
<Form className="w-100" onSubmit={handleSubmit}>
<Form.Group>
<Form.Label>
Enter your Id
</Form.Label>
<Form.Control type="text" ref={idRef} required/>
</Form.Group>
<Button type="submit" style={{marginRight: "5px", marginTop: "5px"}}>Login</Button>
<Button variant="secondary" style={{marginTop: "5px"}}>Create A New Id</Button>
</Form>
</Container>
)
}
It appears that the error is in the App.js on the onSubmit={setId()} because when I comment it out it works.
The idea here is that when you click the "Login" button the value in the <Form.Control type="text" ref={idRef} required/> is stored in the useState from the App.js. Can I do this without getting the Error: Too many re-renders. React limits the number of renders to prevent an infinite loop.?
Just use setId without parenthesis (). When you write the parenthesis beside a function, it means you are actually calling the function, which is causing rerenders. But in your case, you just want to pass the reference of your function to your Child Component.
<Login onIdSubmit={setId}/>
And also rename onSubmit to onIdSubmit as #Vaibhav commented.

Using input state in different components

I want to use the state of the input in another component in React. So I have a component and in this component, I have a form.
import React, { useState } from "react";
import { Form } from "react-bootstrap";
function Jumbotron() {
const [inputFrom, setInputFrom] = useState("");
return (
<Form className="shadow p-3 mb-5">
<Form.Group controlId="formGroupFrom">
<Form.Label className="form-subtitle">Title</Form.Label>
<Form.Control
type="text"
placeholder="Enter input"
onChange={(event) => setInputFrom(event.target.value)}
/>
</Form.Group>
</Form>
);
}
export default Jumbotron;
So I want to use the 'inputFrom' in other component and I just want to show it. I will do it in several components so could you give me an advice about it?
import React from "react";
import { Container } from "react-bootstrap";
function Checkout() {
return (
<Container>
<div className="shipment-info">
<div className="basic">
<div className="col-2 title">From</div>
<div className="col-4 info">{inputFrom}</div>
</div>
</div>
</Container>
);
}
export default Checkout;
You can pass down the prop to another component like so
<NextComponent propYouWantToPass={inputFrom} />
and use it in the next component like props.propYouWantToPass, assuming you got the props in the next component settings props as a parameter
const NextComponent = (props) => {
return(
<View>
<Text>{props.propYouWantToPass}</Text>
</View>
)
}
As other already said, if you are planning to use Checkout component inside Jumbotron component as a child.
The solution is easy, you just pass inputForm as prop to Checkout and receive or inherit from Jumbotron
Could be something like this:
import React, { useState } from "react";
import { Form } from "react-bootstrap";
import { Checkout } from "./Checkout";
function Jumbotron() {
const [inputFrom, setInputFrom] = useState("");
return (
<>
<Form className="shadow p-3 mb-5">
<Form.Group controlId="formGroupFrom">
<Form.Label className="form-subtitle">Title</Form.Label>
<Form.Control
type="text"
placeholder="Enter input"
onChange={(event) => setInputFrom(event.target.value)}
/>
</Form.Group>
</Form>
{/* <-- give any name instead "inputData" */}
<Checkout inputData={inputForm} />
</>
);
}
export default Jumbotron;
Found your inputForm on Checkout as props.inputData
*Ref: https://reactjs.org/docs/components-and-props.html#rendering-a-component

Use react-input-mask with antd in react-final-form

I would like to use react-input-mask with Ant Design Input in react-final-form. In order to use antd with react-final-form I also had to install redux-form-antd. So the file looks like this:
import React from "react";
import ReactDOM from "react-dom";
import { Button } from "antd";
import { Form, Field } from "react-final-form";
import InputMask from "react-input-mask";
import { TextField } from "redux-form-antd";
import "antd/dist/antd.css";
const onSubmit = async values => {
window.alert(JSON.stringify(values, 0, 2));
};
const Input = props => <InputMask {...props} />;
function App() {
return (
<Form
onSubmit={onSubmit}
render={({ handleSubmit, values }) => (
<form onSubmit={handleSubmit}>
<Field
name="mask"
parse={value =>
value
.replace(/\)/g, "")
.replace(/\(/g, "")
.replace(/-/g, "")
.replace(/ /g, "")
}
render={({ input, meta }) => (
<div>
<label>mask phone</label>
<Input mask="+7 (999) 999-99-99" {...input} />
{meta.touched && meta.error && <span>{meta.error}</span>}
</div>
)}
/>
<Field
name="antd"
component={TextField}
label="antd phone"
placeholder="Phone"
/>
<Button className="submit-button" type="primary">
Send
</Button>
<pre>{JSON.stringify(values, 0, 2)}</pre>
</form>
)}
/>
);
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Here is a codesandbox example.
I could only get to work a regular input with an InputMask (input 1) or an antd input without a mask (input 2).
How can I add an InputMask to antd input?
I've managed to use react-input-mask with antd and react-final-form without any external libraries.
Here is my component:
import React from "react";
import InputMask from "react-input-mask";
import { Input } from "antd";
import FormItem from "antd/lib/form/FormItem";
const MaskInput = props => {
const { disabled, mask, label, meta, required } = props;
return (
<FormItem
label={label}
validateStatus={
meta.touched ? (meta.error ? "error" : "success") : undefined
}
help={meta.touched ? (meta.error ? meta.error : undefined) : undefined}
hasFeedback={meta.touched ? true : false}
required={required}
>
<InputMask
mask={mask}
disabled={disabled}
autoComplete="off"
{...props.input}
>
<Input />
</InputMask>
</FormItem>
);
};
export default MaskInput;
Then it is passed to the component prop of the Field:
<Field
name="phone"
label="Phone"
component={MaskInput}
mask="+7 (999) 999-99-99"
required
/>
Here is the link to the codesandbox example.
I've never used either of those libraries, but you might want to check out using format-string-by-pattern with react-final-form's built-in parsing and formatting functionality to achieve a similar thing.
I bet you could throw redux-form-antd components into here pretty easily...

Resources