reactjs

Map function in react (err: TypeError: e.map is not a function)

陌路散爱 提交于 2021-02-09 10:57:07
问题 I want to render items from props, I can do it with initial state, but not with response from server. My render function : const { data } = this.props; return ( <div > {data.map((item, index) => <div key={index} className="row"> <span data = { data } className="number col-4 col-md-8">{item._id}</span> <span data = { data } className="date col-4 col-md-2">{item.date}</span> <span data = { data } className="tag col-4 col-md-2">{item.tag}</span> <div className="col-md-12 "> {item.text} </div> <

Map function in react (err: TypeError: e.map is not a function)

China☆狼群 提交于 2021-02-09 10:56:40
问题 I want to render items from props, I can do it with initial state, but not with response from server. My render function : const { data } = this.props; return ( <div > {data.map((item, index) => <div key={index} className="row"> <span data = { data } className="number col-4 col-md-8">{item._id}</span> <span data = { data } className="date col-4 col-md-2">{item.date}</span> <span data = { data } className="tag col-4 col-md-2">{item.tag}</span> <div className="col-md-12 "> {item.text} </div> <

Why, exactly, do we need React.forwardRef?

早过忘川 提交于 2021-02-09 10:51:05
问题 Let's say I have a component with a scrollable subcomponent, and I want to expose the ability to scroll: const MyComponent = (props) => { return <ScrollView ... /> } I want to be able to do <MyComponent ref={myRef} /> ... myRef.scrollTo({x: 0}); So I need a way to forward the ref to the <ScrollView> . Let's try putting the ref on the props: const MyComponent = (props) => { return <ScrollView ref={props.scrollRef} ... /> } ... <MyComponent scrollRef={myRef} /> ... myRef.scrollTo({x: 0}); I

react-native-contacts returns undefined

怎甘沉沦 提交于 2021-02-09 10:50:47
问题 I'm tryna get the contacts list of the device but react-native-contacts returns undefined. I've Installed the package and linked it as mentioned in the project's GitHub page https://github.com/rt2zz/react-native-contacts I've also added the permissions to the android-manifest file. How do I get it works 回答1: This happens mostly because of the linking-step was missing as there are native modules required: After npm install react-native-contacts (or yarn add ) you are required to link the stuff

Passing parameters to a props function in React

时光怂恿深爱的人放手 提交于 2021-02-09 10:49:32
问题 I wrote two sample components to show what I'm trying to do. If the call was from the same class, I could do like below. onClick={this.myFunc.bind(this, param1, param2)} How do I do the same thing from a stateless component without the need to mess with binding 'this'. onClick={props.onClick['need to add params']} import React from 'react'; class BigComponent extends React.Component{ constructor(props){ super(props); this.handleClick = this.handleClick.bind(this); } handleClick(param1, param2

Typescript - make an optional property required when another property is present

自闭症网瘾萝莉.ら 提交于 2021-02-09 10:38:15
问题 I have proptypes shown below interface Props { resource: string; create?: boolean; route?: string; } As can be seen above, create and route are optional props. However, I would like to implement the types such that if create prop is present then route must now be required. Anyone have any ideas how to do this? 回答1: You should group these props and make that grouped prop optional as interface RouteProps{ create: boolean; route: string; } interface Props { resource: string; routeInfo?:

Typescript - make an optional property required when another property is present

假装没事ソ 提交于 2021-02-09 10:35:19
问题 I have proptypes shown below interface Props { resource: string; create?: boolean; route?: string; } As can be seen above, create and route are optional props. However, I would like to implement the types such that if create prop is present then route must now be required. Anyone have any ideas how to do this? 回答1: You should group these props and make that grouped prop optional as interface RouteProps{ create: boolean; route: string; } interface Props { resource: string; routeInfo?:

Typescript - make an optional property required when another property is present

不羁岁月 提交于 2021-02-09 10:33:06
问题 I have proptypes shown below interface Props { resource: string; create?: boolean; route?: string; } As can be seen above, create and route are optional props. However, I would like to implement the types such that if create prop is present then route must now be required. Anyone have any ideas how to do this? 回答1: You should group these props and make that grouped prop optional as interface RouteProps{ create: boolean; route: string; } interface Props { resource: string; routeInfo?:

Updating state in reducer using variables

旧时模样 提交于 2021-02-09 10:32:56
问题 I'm build a simple app that expands and collapses sections of content based on their state. Basically, if collapse = false, add a class and if it's true, add a different class. I'm using Next.js with Redux and running into an issue. I'd like to update the state based on an argument the action is passed. It's not updating the state and I'm not sure why or what the better alternative would be. Any clarification would be great! // DEFAULT STATE const defaultState = { membership: 'none',

What is producing “Invalid option for project: true” when running 'tslint --project' on a React project?

╄→гoц情女王★ 提交于 2021-02-09 09:21:17
问题 I'm trying to set up tslint to work on a small sample React/Typescript project, following a tutorial online. When I either run yarn lint or simply enter tslint --project in the terminal, I keep getting the error Invalid option for project: true I've done a lot of googling, and I can't find what I'm doing wrong. My tslint.json is { "extends": [ "tslint:recommended", "tslint-react", "tslint-config-prettier" ], "rules": { "ordered-imports": false, "object-literal-sort-keys": false, "no-console":