react-jsx

React component returning cannot read property '__reactInternalInstance$ of null after trying to access array

孤街醉人 提交于 2019-11-26 21:35:01
问题 Here is the problematic component in question. const UserList = React.createClass({ render: function(){ let theList; if(this.props.data){ theList=this.props.data.map(function(user, pos){ return ( <div className="row user"> <div className="col-xs-1">{pos}</div> <div className="col-xs-5">{user.username}</div> <div className="col-xs-3">{user.recent}</div> <div className="col-xs-3">{user.alltime}</div> </div> ); }, this); } else { theList = <div>I don't know anymore</div>; } console.log(theList);

How to have conditional elements and keep DRY with Facebook React's JSX?

被刻印的时光 ゝ 提交于 2019-11-26 21:21:54
How do I optionally include an element in JSX? Here is an example using a banner that should be in the component if it has been passed in. What I want to avoid is having to duplicate HTML tags in the if statement. render: function () { var banner; if (this.state.banner) { banner = <div id="banner">{this.state.banner}</div>; } else { banner = ????? } return ( <div id="page"> {banner} <div id="other-content"> blah blah blah... </div> </div> ); } Jack Allan Just leave banner as being undefined and it does not get included. What about this. Let's define a simple helping If component. var If =

Removing an item causes React to remove the last DOM node instead of the one associated with that item

一世执手 提交于 2019-11-26 20:38:38
问题 I was trying to animate list insertion and removal with ReactCSSTransitionGroup, but the removal animation always animates only the last item of the list instead of the one that's being removed. Here's a jsbin to illustrate this problem. Try pressing the "Add" button to verify that the insertion animation indeed works as expected, then click on the "x" besides any item to see the problem where the last item of the list is animated instead of the one you tried to remove. Did I do something

ReactJS - Add custom event listener to component

六眼飞鱼酱① 提交于 2019-11-26 19:34:26
In plain old javascript I have the DIV <div class="movie" id="my_movie"> and the following javascript code var myMovie = document.getElementById('my_movie'); myMovie.addEventListener('nv-enter', function (event) { console.log('change scope'); }); Now I have a React Component, inside this component, in the render method, I am returning my div. How can I add a event listener for my custom event? (I am using this library for TV apps - https://github.com/ahiipsa/navigation ) import React, { Component } from 'react'; class MovieItem extends Component { render() { if(this.props.index === 0) { return

Value of this in React event handler

你离开我真会死。 提交于 2019-11-26 17:45:25
For some reason the value of this is being lost in react event handler. Reading the docs I thought that react did some stuff here to make sure this was set to the correct value The following doesn't work as I'd expect import React from 'react'; export default class Observer extends React.Component { handleClick() { console.log(this); //logs undefined } render() { return ( <button onClick={this.handleClick}>Click</button> ); } } But this does: import React from 'react'; export default class Observer extends React.Component { handleClick() { console.log(this); //logs Observer class instance }

Visual Studio 2015 JSX/ES2015 syntax highlighting

自作多情 提交于 2019-11-26 16:01:41
How can I get a proper syntax highlighting in Visual Studio 2015 for JSX with ES2015 code? It works fine if I remove the import and export keywords: I just updated to Visual Studio 2015 Enterprise Update 1 but it still remains the same. Adam Weber UPDATE (2017-02) Node Tools for Visual Studio (NTVS) has been using the Salsa analysis engine since v1.2 and using NTVS is likely the path of least resistance for JSX support. https://github.com/Microsoft/nodejstools Read (and upvote) this answer for more details: https://stackoverflow.com/a/41996170/9324 ORIGINAL ANSWER I ran into the same issue and

ReactJs: Prevent multiple times button press

倖福魔咒の 提交于 2019-11-26 15:31:46
问题 In my React component I have a button meant to send some data over AJAX when clicked. I need to happen only the first time, i.e. to disable the button after it's first use. How I'm trying to do this: var UploadArea = React.createClass({ getInitialState() { return { showUploadButton: true }; }, disableUploadButton(callback) { this.setState({ showUploadButton: false }, callback); }, // This was simpler before I started trying everything I could think of onClickUploadFile() { if (!this.state

Insert HTML with React Variable Statements (JSX)

杀马特。学长 韩版系。学妹 提交于 2019-11-26 11:10:21
I am building something with React where I need to insert HTML with React Variables in JSX. Is there a way to have a variable like so: var thisIsMyCopy = '<p>copy copy copy <strong>strong copy</strong></p>'; and to insert it into react like so, and have it work? render: function() { return ( <div className="content">{thisIsMyCopy}</div> ); } and have it insert the HTML as expected? I haven't seen or heard anything about a react function that could do this inline, or a method of parsing things that would allow this to work. Douglas You can use dangerouslySetInnerHTML , e.g. render: function() {

Can you force a React component to rerender without calling setState?

本秂侑毒 提交于 2019-11-26 11:01:34
I have an external (to the component), observable object that I want to listen for changes on. When the object is updated it emits change events, and then I want to rerender the component when any change is detected. With a top-level React.render this has been possible, but within a component it doesn't work (which makes some sense since the render method just returns an object). Here's a code example: export default class MyComponent extends React.Component { handleButtonClick() { this.render(); } render() { return ( <div> {Math.random()} <button onClick={this.handleButtonClick.bind(this)}>

How to loop and render elements in React.js without an array of objects to map?

巧了我就是萌 提交于 2019-11-26 10:07:36
问题 I\'m trying to convert a jQuery component to React.js and one of the things I\'m having difficulty with is rendering n number of elements based on a for loop. I understand this is not possible, or recommended and that where an array exists in the model it makes complete sense to use map . That\'s fine, but what about when you do not have an array? Instead you have numeric value which equates to a given number of elements to render, then what should you do? Here\'s my example, I want to prefix