Why am I unable to console.log() anything in react? [closed] - reactjs

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
import React from "react";
export default class App extends React.Component {
render() {
console.log('Hello');
return (
<div> App component </div>
)
}
}
I am facing this issue since yesterday. Hello should have been printed in the console on inspecting my website but nothing is getting displayed. I saved the code and also refreshed the browser.

Open your browser inspection and select the console tab on it. Click on the All levels and make sure all the options are selected:

Related

Cannot access `variable` before initialization when using `screen.getByTestId()` in React Testing Library [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed yesterday.
Improve this question
(PS: my question is solved but closed. So I updated this question to make it simpler, but still replicating the same problem as before)
I am very new to React and now I am learning how to test React Components with React Testing Library.
I have made an Education Component that would render education details. But for this question I will keep the Component very simple to replicate the error.
I want to test if Education is rendered correctly with toBeInTheDocument() but I came across a problem while using getByTestId() to select the Education Component as a whole. I got the Reference Error: Cannot access 'Education' before initialization error.
My Education.js Component:
class Education extends React.Component {
render() {
return (
<div data-testid='Education' id='Education'>
My Education
</div>
)
}
My Education.test.js:
import { render, screen } from '#testing-library/react'
import '#testing-library/jest-dom'
describe('Test Components are rendering correctly', () => {
it('renders Education Component', () => {
render(<Education />)
const Education = screen.getByTestId('Education')
expect(Education).toBeInTheDocument();
})
})
Error Screenshot:
Please let me know what I am doing wrong or should I provide more information to solve this problem. Thank you in advance, I appreciate it!

Map method not working for firestore based Next app [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I'm trying to return a simple JSX component based on Firebase data but it doesn't seem to work. I tried returning only the component and that works perfectly. However, if I try to map it, it doesn't work. The server is returning data for sure as I could log it into the console.
I tried to set up the code in Codesandbox but that isn't being setup properly. Any help is much appreciated.
This is my Github link: https://github.com/servesh-chaturvedi/Chat-App-with-firebase
The current code in CodeSandBox: https://codesandbox.io/s/small-morning-clnrp?file=/components/Sidebar.js
{chatSnapshot?.docs.map((chat) => {
<Chat key={chat.id} id={chat.id} users={chat.data().users} />
})}
When you use an arrow function, the default behavior is to return whatever is to the immediate right of the arrow, such that:
const returnsHello = () => 'Hello'
returnsHello() //'Hello'
But when you want to use the arrow function with a block of code like you're doing with the curly braces, the implied return is dropped and must be made explicit. Here's an edit of your code that should work, assuming you don't have other issues.
{chatSnapshot?.docs.map((chat) => {
return <Chat key={chat.id} id={chat.id} users={chat.data().users} />
})}
This will iterate through each element of your array, and return a new array with Chat components using the array values.

How can I transfer Props from one component to another via Link [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
My Code
The code shows that when you enter data in three lines, they must be transmitted via Link using the State method, but when you try to display them on the secStr page, it gives Undefined
You have to add the withRouter HOC (high order component) into secStr page to have access to location props and the state data
Take a look at this:
https://reactrouter.com/web/api/withRouter
Something like this:
import React, {
Component
} from "react";
import {
withRouter
} from "react-router";
class secStr extends Component {
render() {
console.log(this.props);
return ( <
div >
date in inputs <
br / >
<
/div>
);
}
}
export default withRouter(secStr);

React open select file dialog using ref not working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to open file dialog using react useRef hook.
when browser executes fileInputRef.current.click() code snippet I'm getting below error in console
error: File chooser dialog can only be shown with a user activation
This works well:
const inputFileRef = useRef();
...
const handleBtnClick = () => {
inputFileRef.current.click();
}
...
<form>
<input type="file" ref={inputFileRef} />
<button onClick={handleBtnClick}>Select file</button>
</form>

Visited state/class on div not anchor using React Js [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to add a visited state/class on a Div by using state, but running into some issues. I am not able to use an anchor, so I need to be able to set it on a class.
This is not an option:
a:visited {
...
}
Codesandbox.io:
https://codesandbox.io/s/l5x1l78mqz
You need to store this information in some state variable. You could store this right on BreadCrumb, but to do that you'd need to (1) make it a class and (2) handle its click function natively before calling onClickTabItem:
class BreadCrumb extends Component {
constructor(props) {
this.state = {visited: false}
}
render () {
return (
<li className={this.state.visited ? "visited" : ""} ... >
);
}
}
Alternatively you can store the visited state information in SummaryBar so that you can pass the visited status in as a prop, as it seems you are attempting to do. To do this you need a data structure that allows you to store all the visited states, not just the currently selected one.
state = { activeTab: 0, select: "" , visited: {}};
Then in handleClickTabItem, you updated the visited for the given element (which you'd need to figure out the title for)
visited = this.state.visited;
visited[title] = true;
this.setState(visited: visited);
To figure out the title, the easiest way is probably to pass it as an arg to the click handler in BreadCrumb
onClick={() => onClickTabItem(title)}
You can then use this in your Buttons function as you are attempting to do:
visited={this.state.visited[title]}

Resources