reactjs

Material-UI: either `image` or `src` property must be specified

老子叫甜甜 提交于 2021-02-18 22:54:52
问题 Its looks like CardMedia need an image while component is created. Since I am pulling the image data via componentDidMount (RestAPI) then the component is already mount. componentDidMount() { // get all items via category ID and owner ID const restApi = new API({ url: 'api' }) restApi.createEntity({ name: 'items' }) // api/items/<categoryId>/<ownerId> restApi.endpoints.items.getTwo({ id_a: this.props.categoryId, id_b: this.props.ownerId }).then(({ data }) => this.setState({ appData: data }))

Typescript setState with computed property names

人走茶凉 提交于 2021-02-18 22:33:07
问题 I am using Typescript 2.1. I have a React component with state with 2 collections of numbers, I don't want to duplicate the addItem and removeItem methods and want them to be generic. This is the code: type Collection = 'challenges' | 'disciplines'; type State = { lang: string; challenges: number[]; disciplines: number[]; } class MyComponent extends React.Component<{}, State> { addItem = (collection: Collection, id: number) => { this.setState({ [collection]: [...this.state[collection], id], }

Using SCSS variables in CSS Modules

风格不统一 提交于 2021-02-18 22:33:06
问题 I've a _color.scss file which consists of 400 colors as variables. And I've like 20 components which needs this color variables in their styles. I'm using SCSS + CSS Modules to construct my styles. As of now, I'm importing this _color.scss in each and every component's style.scss . Example: component1.scss @import "color.scss"; component2.scss @import "color.scss"; component3.scss @import "color.scss"; Previously, when I was using SCSS standalone, I'll import the color.scss to my index.scss

Typescript setState with computed property names

落爺英雄遲暮 提交于 2021-02-18 22:32:08
问题 I am using Typescript 2.1. I have a React component with state with 2 collections of numbers, I don't want to duplicate the addItem and removeItem methods and want them to be generic. This is the code: type Collection = 'challenges' | 'disciplines'; type State = { lang: string; challenges: number[]; disciplines: number[]; } class MyComponent extends React.Component<{}, State> { addItem = (collection: Collection, id: number) => { this.setState({ [collection]: [...this.state[collection], id], }

TypeScript: Interface cannot simultaneously extends two types

拥有回忆 提交于 2021-02-18 22:31:24
问题 I'm writing a React app using TypeScript. I use material-ui for my components. I'm writing a custom wrapper for material-ui's Button component. It looks like this: import MUIButton, { ButtonProps } from "@material-ui/core/Button"; import withStyles, { WithStyles } from "@material-ui/core/styles/withStyles"; import classNames from "classnames"; import React, { PureComponent } from "react"; import styles from "./styles"; export interface OwnProps { color: "primary" | "danger" | "warning" |

React Routing Redirect onClick

喜夏-厌秋 提交于 2021-02-18 22:30:07
问题 Ive been trying to do a simple redirect to another component on button click, but for some reason it doesnt work.I want to redirect to '/dashboard' and display AdminServices from login as follows: //index.js ReactDOM.render(<BrowserRouter><App /></BrowserRouter>, document.getElementById("root")); //App.js <Switch> <Route path="/" component={Login} /> <Route path="/dashboard" component={AdminServices} /> </Switch> //Login.js <Button onClick={this.login} > </Button> login = () =>{ <Redirect to=

compose not exported from react-apollo

佐手、 提交于 2021-02-18 22:15:26
问题 I'm following a graphql tutorial on youtube (https://www.youtube.com/watch?v=ed8SzALpx1Q at about 3hr 16min) and part of it uses compose from "react-apollo". However, I'm getting an error because the new version of react-apollo does not export this. I read online that I need to replace import { compose } from "react-apollo" with import { compose } from "recompose" but doing that produces the error TypeError: Cannot read property 'loading' of undefined I've also read that I should replace the

Code splitting/react-loadable issue

泪湿孤枕 提交于 2021-02-18 22:10:29
问题 I'm trying to introduce code splitting into my app using react-loadable. I tried it on a very simple component: const LoadableComponent = Loadable({ loader: () => import('components/Shared/Logo/Logo'), loading: <div>loading</div>, }); However, when this component is rendered, I get the following error: Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of

Should I wrap all functions that defined in component in useCallback?

半世苍凉 提交于 2021-02-18 21:57:58
问题 As far as I've known, functions that defined in a React's functional component are regenerated whenever the component rerenders. Since useCallback can be triggered by specific dependencies, it prevents unnecessary regeneration of these functions. Should I wrap each of them in useCallback, and pass relevant dependencies? import React from 'react' const Comp = () => { const fn1 = useCallback( ()=>{ // do something    }, [dependency1]) const fn2 = useCallback( ()=>{ // do something    },

Should I wrap all functions that defined in component in useCallback?

删除回忆录丶 提交于 2021-02-18 21:57:40
问题 As far as I've known, functions that defined in a React's functional component are regenerated whenever the component rerenders. Since useCallback can be triggered by specific dependencies, it prevents unnecessary regeneration of these functions. Should I wrap each of them in useCallback, and pass relevant dependencies? import React from 'react' const Comp = () => { const fn1 = useCallback( ()=>{ // do something    }, [dependency1]) const fn2 = useCallback( ()=>{ // do something    },