reactjs

Fetch request in class component is not returning data or updating state

风格不统一 提交于 2021-02-11 14:14:56
问题 I have refactored a component to add a fetch request to an AWS database, the data is accessible and a quick mock on codesandbox works. However when used in this context it errors as... TypeError: Cannot read property 'forEach' of undefined Unhandled Rejection (TypeError): this.setState is not a function and in the console... {error: null, isLoaded: true, users: Array(0), undefined} code below... constructor() { this.mapUserCountries = {}; this.state = { error: null, isLoaded: true, users: []

Fetching remote images and converting them to gatsby images

≡放荡痞女 提交于 2021-02-11 14:14:06
问题 I'm trying to fetch some images from WP because Gatsby doesn't work with woocommerece yet. My plugin below is able to convert the images and add them to .cache when building, but it doesn't add a field of localFile___NODE within the Gatsby graphQl. It doesn't seem to be adding any nodes at all for me to query with ImageSharp plugin. Graphql shows them being processed under ALLFiles but not within the wcProduct node I've created in Graphql...what is going on that this plugin is not creating

How to trick react router into thinking `/` is the mount directory?

旧时模样 提交于 2021-02-11 14:13:49
问题 Let's say I have a simple react app: import React from 'react'; import ReactDOM from 'react-dom'; import {BrowserRouter as Router, Route} from 'react-router-dom' import * as serviceWorker from './serviceWorker'; import App from './App'; import About from './components/pages/About' import Header from './components/layout/Header'; ReactDOM.render( <React.StrictMode> <Router> <Route path = "/" component={Header} /> <Route exact path="/" component= {App} /> <Route path="/about" component = {About

How to use Context API to pass down a state while using a Router in React JS

自作多情 提交于 2021-02-11 14:11:04
问题 I have a Context API file setup which has a state and a function which fetches data from an API and sets the state, and i want to pass the state down to my other components. In my App.js, I am using React-Router to specify the routes. How do i pass the state down to these components using Context API, whilst using React-Router. My ApiContext.js file looks like this : import React, {useState, createContext } from 'react'; export const ApiContext = createContext(); export const ApiProvider =

Problem with NPX create-react-app. Keeps getting stuck at “Found 0 Vulnerabilties”

故事扮演 提交于 2021-02-11 14:09:39
问题 So I am trying to create a new React project using npx create-react-project but it keeps getting stuck after downloading the packages. So far the folder only contains the node modules and the 2 package.json files. The create-react-app goes well until it reaches found 0 vulnerabilities. I have done this in different folders and different hard drives but the same result. It just doesn't go further. I also deleted my previous global create-react-app installation but this didn't make a difference

React componentWillUpdate getting called twice

血红的双手。 提交于 2021-02-11 14:05:13
问题 Iam new to React and i came across with this doc. It says that: Either way, it is unsafe to use componentWillUpdate for this purpose in async mode, because the external callback might get called multiple times for a single update How is it possible for the componentWillUpdate to be called multiple times? It doesnt explain it. Thank you 回答1: On async mode, your component update/render may be postponed so react can deliver some hi-priority stuff. This means willUpdate will be called each time

How to clear leaflet map in reactjs by button?

自作多情 提交于 2021-02-11 14:03:19
问题 I want to clear my leaflet map by clicking a button...how to do this... I want to clear all drawed shapes on the map, so the map to be clear This is my code of the leaflet map in the return statement: <Map key={this.state.keyMAP} style={{ height: "50vh" }} center={position} zoom={13} onClick={this.handleClick} onCreate={this.onCreate} onLocationfound={this.handleLocationFound} > {/* startDirectly */} <TileLayer attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'

useState is accepting updates if the object reference does not change

大兔子大兔子 提交于 2021-02-11 14:00:54
问题 I thought that useState needed to have a new value for it to accept updates: I have created thiscodesandbox to illustrate what I mean. I have a nested data structure like this const state = { name: "parent", data: { isExpanded: false, children: [ { name: "one", data: { isExpanded: false } }, { name: "two", data: { isExpanded: false } }, { name: "three", data: { isExpanded: false } } ] } }; I then use useState to cause rerenders when the state changes: function App() { const [tree, setTree] =

Use React in CMS and expose functions to HTML to access useState functions in React

两盒软妹~` 提交于 2021-02-11 14:00:50
问题 I want to use React in a CMS. With js I inject React into a div that displays something comming from React. That works well but only because React creates the inputfields too. Now I need the CMS create the input fields and want to attach an event to the created input in the CMS template. <input type="number" onchange="exposedReact()">something</input> In react I would have somewhere definied a function: const [number, setNumber] = useState(0); const exposedReact = (e) => { setNumber(e.target

How to send all request to index.html - reactjs

↘锁芯ラ 提交于 2021-02-11 14:00:33
问题 I'm using linux OS and apache. I created a webapp directory inside public_html . inside public_html I have a landing page. I want to keep this landing page. all resources can fetch as well. but I got Page not found error. I found out that I should send all requests to index.html file. .htaccess file: <ifModule mod_rewrite.c> ####################################################################### # GENERAL # ####################################################################### # Make apache