News Articles react-component

Trouble importing images in React.js

So im new with react and i have made a react project using create react app. Im trying to import an image to use in one of my components. I know that in react to use images you import them using an import statement with the relative path to your image. However whenver i try […]

Props dont get passed

Im trying to make an emoji search app with react just for practice,I have three components, the first one is ‘APP’ and then an ‘SearchBar’ component that renders a search bar and also renders the third component which is ‘EmojiContainer’ that’s supposed to display the emojis based on the props passed to it from the […]

Props dont get displayed

Im trying to make an emoji search app with react just for practice,I have three components, the first one is ‘APP’ and then an ‘SearchBar’ component that renders a search bar and also renders the third component which is ‘EmojiContainer’ that’s supposed to display the emojis based on the props passed to it from the […]

JSX doesn’t honor usestate function before sleep

Posting the minimal reproducible example : say I have a simple web page with text area, whenever I click on button the function handleUPClick is called. Inside that function I have given a sleep of 2seconds, although the settext after sleep is honored but setText("Converting to Upper case…"); before the sleep function is not getting […]

Must React components be in hierarchy?

I have always been told that React components should be designed in a tree hierarchy so that the ancestor/parents can hold the state and pass it to their children. I just wonder, is that a MUST? The design principal of React is that "React has been designed from the start for gradual adoption, and you […]

Is it possible to use spread out operator to set the react component props?

I wanted to use spread operator to set the component props. This is mainly because I don’t want to use state for each of the props value. Is this possible? The code below returns an error saying that the props are missing. Thanks in advance! const compProps = { label: ‘Test Label’, value: ‘Test Value’, […]

How to manipulate object property on a reusable component in React

Im using MUIs input field for a lot of input sections of my questionnaire and want to save it to my state variable that is an object which holds all my form values. How can I manipulate my formData object in a reusable MUI component? Im currently passing formData as a prop to the component […]

Converting function React component to class component

I’m trying to add a login page to my existing app. Following along this tutorial https://www.digitalocean.com/community/tutorials/how-to-add-login-authentication-to-react-applications Got stuck in step 3. My app is a class component, while in the tutorial it’s a function component. How can I convert this to a class component? import React from ‘react’; import { BrowserRouter, Route, Switch } from […]

Directly redirect to AAD login page on hitting the URL in browser

Using MSAL 2.0 in React we can easily achieve redirect to login page using below methods Login with pop-up Login with redirect But both the methods can be performed on a login/signin button action. In my case, I want to redirect my React app directly to login page, when the URL is hit in the […]

Converting useState to a class component

Trying to add a login page to my existing react app. Was following along this tutorial https://www.digitalocean.com/community/tutorials/how-to-add-login-authentication-to-react-applications Got stuck in step 1, because we can’t use hooks in class components. My app is a class component. So, I was wondering how can I create an equivalent to this in class component? const [token, setToken] = […]

Why is my React js component calling ComponentDidMount twice and erasing my router URL params?

I am navigating from my web app’s home screen to a user’s profile page once their profile is selected. I am passing the selected user’s userID as a router parameter in order to load their data on their personal page. goProfile = (userID) => { this.props.history.push(‘/galleries/’ + userID); } This is that page’s route. <Route […]

REACT wait for useEffect to complete before rendering the UI

interface MyValue { //interface declaration } export function MyComponent { const [myvalue, setMyvalue] = useState<MyValue>() useEffect(() => { setMyvalue(passedData) }, [passedData]) function getAutofocus() { // return true/false based on myvalue value } render() { return ( <div> <input autofocus={getAutofocus()} ref={c => (this._input = c)} /> </div> ); } } } passedData is passed as a […]

Passing Props to render

I am new to React, and I am trying to tackle a problem I am facing. It is to serve as an FAQs page with a hierarchy of tabs. So far it renders all of my tabs out, but I want to render tabs underneath based on if the last tab is active. Essentially it […]

Passing State data to other component

I currently have a child component MoodCard which is being rendered in parent component MoodPage. I have a State Hook in the parent component const [isHover, setIsHover] = useState(false) which toggles between true or false when the MoodCard is hovered over. In the MoodCard component, I have a conditional {isHover && ( <div className="play-button-container"> <i […]

How to pass function from parent component to child component with type assingable

I new at typescript, i want to pass my function onDogSelected to the children component <Dogs /> But i got some error message like this Type ‘{ onDogSelected: (e: any) => void; }’ is not assignable to type ‘IntrinsicAttributes & { children?: ReactNode; }’.Property ‘onDogSelected’ does not exist on type ‘IntrinsicAttributes & { children?: ReactNode; […]

How to call parent component method (close menu) from a child component (menu item) when in separate files?

I’m trying to build a generic title bar at the top of every page, featuring the title of the page and an option menu (if any) which is a dropdown list attached to 3 vertical dots. The Menu of the option menu is part of TitleBar.tsx, the items which should be featured within the Menu […]

react component not working properly when rendered multiple times

Given below is code of a react component(Newsitem) and I have passed title as props through API. In Newsitem there is a span tag which I need to make hidden when the title passed in Newsitem contains less that 70 characters. But that doesn’t happen, what happens is that whenever title has less than 70 […]

React: Having trouble loading external data and passing it down to component after storing it in Redux

So here’s the situation: I have a quiz app, I want to load the questions from json-server and store them in Redux state. Why error happens: The app crashes before useEffect can load the data, because I’m passing down the questions data as a prop Here’s my useEffect function that loads the data: useEffect(async () […]

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.