react-jsx

Is there a way to tell if ReactElement renders “null”?

ぐ巨炮叔叔 提交于 2019-12-05 00:31:27
In my JSX, I have a case of a conditional rendering logic - when element A renders something (it's render() function returns something other than null ), then also render element B, just above the element A. Code example (simplified) would look like this: function render() { let elemA = (<ElementA someProp={this.someVar} />); if (elemA.isNull()) { return ( <div> { ...someElements } </div> ); } return ( <div> { ...someElements } <ElementB /> { elemA } </div> ); } So my question is - Is there any way to have the elemA.isNull() check? No, there's no way to determine what a child will render using

ReactJS: Control a child state from the child and the parent

泄露秘密 提交于 2019-12-05 00:25:05
I have a rather simple problem and I'm not sure how to solve it with React's one way data flow. Say you have a link in the parent that shows a modal In the modal, you have an "X" that closes it. I know I can change the state of the modal from the parent via props // In the parent <Modal display={this.state.showModal} /> // In the modal <div className={this.props.display ? "show" : "hide"}> <a className="close">×</a> ... </div> And I know how to close the modal, but not both. Not sure how to keep a state that is shared and controllable by both the parent and the child modal. UPDATE In trying to

Mysterious ESLint Parsing Error

狂风中的少年 提交于 2019-12-05 00:03:59
On line 4 of the following code, ESLint is giving me a parsing error saying: Unexpected token = I'm wondering why this is the case? The code runs properly. What am I doing wrong? import { Component, PropTypes } from 'react'; export default class MainApp extends Component { static propTypes = { children: PropTypes.any.isRequired } componentWillMount() { require('./styles/main.styl'); } render() { return ( <div> {this.props.children} </div> ); } } You cannot have properties inside classes, you can only have methods. Reference: http://www.2ality.com/2015/02/es6-classes-final.html#inside_the_body

PHPStorm JSX/React syntax highlighting

一曲冷凌霜 提交于 2019-12-04 22:18:38
I'm using PHPStorm 8.0.3 for my current project, but unfortunately it doesn't support JSX. In my React components (which are then compiled by Browserify) HTML gets underlined in red and invalidated: This is just a small component, but it surely gets pretty annoying with bigger ones. Also code formatting doesn't work as expected. Is there a (hard to find) setting which enables the correct syntax highlighting? If there is no such setting, is it possible to download a corresponding package? If there is no support at all, how do I extend PHPStorm (maybe by a custom Color/Syntax Schema) to accept

Typescript custom @types package for @types/react-router

我的未来我决定 提交于 2019-12-04 19:55:52
My project use react with typescript. I need to use react-breadcrumbs to show Breadcrumb for react-router. Now I add two @type package to My package.json "dependencies": { "@types/react-breadcrumbs": "^1.3.7", "@types/react-router": "^3.0.8" } react-breadcrumb need to use route.props.name to show name for breadcrumb, but when I use @type package in npm the Route.d.ts file has not name Props in the interface RouteProps. interface RouteProps extends IndexRouteProps { path?: RoutePattern; } interface IndexRouteProps { component?: RouteComponent; components?: RouteComponents; getComponent?:

If the props for a child component are unchanged, does React still re-render it?

狂风中的少年 提交于 2019-12-04 19:14:48
问题 Suppose I have the following pairing of parent and child components in React: var ChildComponent = React.createClass({ getDefaultProps: function(){ return { a: 'a', b: 'b', c: 'c' } }, render: function() { return ( /* jshint ignore:start */ <div className={'child' + (this.props.b ? ' modifierClass' : '')} something={this.props.a}> {this.props.c} </div> /* jshint ignore:end */ ); } }); var ParentComponent = React.createClass({ componentDidMount: function(){ //After 10 seconds, change a

How to properly import React JSX from separate file in Typescript 1.6

做~自己de王妃 提交于 2019-12-04 18:58:49
问题 I have the following app.tsx file working fine loading an App element from a React.Component and a child Worklist element from another React.Component (both classes defined in the same app.tsx file). This is running in Visual Studio with Typescript 1.6 installed (ECMAScript version: ECMAScript 5, JSX compilation: React, Module System: CommonJS). However, I would like to split these two components into separate files. However, when I uncomment the import of WorkList and remove the class

W3C HTML validation for React JSX files

时光总嘲笑我的痴心妄想 提交于 2019-12-04 18:45:15
问题 When working on front-end projects I always like to work with linting tools. They prevent from dumb mistakes to serious smelly code pieces. Linting tools also suggest improvements and optimisations. Validating and linting HTML means using the W3C Validator. When working with vanilla JavaScript projects I use the grunt-html Grunt NPM module. And when working with Angular 1.x I use the grunt-html-angular-validate module, which is the same validator but adapted to Angular requirements (non

React.JS - multiple elements sharing a state ( How do I modify only one of the elements without affecting the others? )

我们两清 提交于 2019-12-04 18:25:13
class App extends Component { constructor(props) { super(props); this.state = { Card: Card } } HandleEvent = (props) => { this.SetState({Card: Card.Active} } render() { return ( <Card Card = { this.state.Card } HandleEvent={ this.handleEvent }/> <Card Card = { this.state.Card } HandleEvent={ this.handleEvent }/> ) } } const Card = props => { return ( <div style={props.state.Card} onClick={ props.HandleEvent}>Example</div> ) } Every time I click on one of the cards all of my elements change states, how do I program this to only change card that I clicked? Here's a working example import React,

Replace part of string with tag in JSX

柔情痞子 提交于 2019-12-04 15:57:35
问题 I'm trying to replace parts of a string with JSX tags, like so: render: function() { result = this.props.text.replace(":",<div className="spacer"></div>); return ( <div> {result} <div> ); } But given that this.props.text is Lorem : ipsum , it results in <div> Lorem [object Object] ipsum. </div> Is there a way to solve this or another way to replace parts of a string with JSX tags? 回答1: When you pass a JSX element to replace() as the second argument, that element is converted to a string