react-jsx

How to render a HTML comment in React?

落花浮王杯 提交于 2019-11-28 09:49:06
Currently the render method can only return a single element/component. See: here In the discussion under that ticket some suggest to wrap multiple elements returned from a React component in a HTML comment so that the wrapping component is ignored by the browser, e.g.: <A> <B></B> <Fragment> <C></C> <D></D> </Fragment> <E></E> </A> would render to: <a> <b></b> <!--<fragment data-reactid="">--> <c></c> <d></d> <!--</fragment>--> <e></e> </a> But how to actually create a component that renders just HTML comment? In other words, how the render function of the 'fragment' component in the example

How do I convert a string to jsx?

本小妞迷上赌 提交于 2019-11-28 09:39:05
How would I take a string, and convert it to jsx ? For example, if I bring in a string from a textarea , how could I convert it to a React element; var jsxString = document.getElementById('textarea').value; What is the process I would use to convert this on the client? Is it possible? user8296051 You can consider using the JSX attribute dangerouslySetInnerHTML Example usage below: class YourComponent{ render() { someHtml = '<div><strong>blablabla<strong><p>another blbla</p/></div>' return( <div className="Container" dangerouslySetInnerHTML={{__html: someHtml}}></div> ) } } I know is to late

Import React vs React, { Component }

断了今生、忘了曾经 提交于 2019-11-28 09:22:59
Import React vs Import React, { Component } Which one is better and why? Or does it make no difference other than writing less code later on? Does writing { Component } mean it only imports the Component object? import React, { Component } lets you do class Menu extends Component instead of class Menu extends React.Component . It's less typing and duplication of the React namespace, which is generally a desired modern coding convention. Additionally, tools like Webpack 2 and Rollup do "tree shaking," meaning any unused exports are not bundled into your final code. With import React / React

True custom attributes (e.g. Microdata) in React

落花浮王杯 提交于 2019-11-28 09:04:17
The site I am developing makes use of Microdata (using schema.org). As we are shifting development over to use React to render our views I have hit a blocker where React will only render attributes in the HTML spec however Microdata specifies custom attributes such as itemscope . As I'm relatively new to React and haven't had chance to fully understand the core just yet, my question is what would be the best way to extend the functionality of react.js to allow for defined custom attributes, e.g., Microdata? Is there a way of extending the attributes/props parser or is it a job for a mixin

babel watch SyntaxError: Unexpected token

 ̄綄美尐妖づ 提交于 2019-11-28 08:46:36
问题 When I uses babel to watch a jsx file. But there is a syntax error. Before that, I uses react-tools to watch, and everything is fine. SyntaxError: assets/js/chat/chat.jsx: Unexpected token (258:16) 256 | if (this.props.isOpen) { 257 | return ( > 258 | <div className="modal-overlay"> | ^ 259 | <ReactCSSTransitionGroup transitionName={this.props.transitionName}> 260 | <div className="chat-modal"> 261 | {this.props.children} The following is my code. var ReactCSSTransitionGroup = React.addons

Which ReactJS syntax to use; React.createClass or ES6 extends?

柔情痞子 提交于 2019-11-28 07:48:11
I'm beginner of ReactJS. I learned and studied a lot of documents and ebooks on various websites. I realize there are two syntaxes for ReactJS. Example: React.createClass({ displayName: 'Counter', getDefaultProps: function(){ return {initialCount: 0}; }, getInitialState: function() { return {count: this.props.initialCount} }, propTypes: {initialCount: React.PropTypes.number}, tick() { this.setState({count: this.state.count + 1}); }, render() { return ( <div onClick={this.tick}> Clicks: {this.state.count} </div> ); } }); And this version is written by ES6: class Counter extends React.Component

React is expected to be globally available

怎甘沉沦 提交于 2019-11-28 07:27:28
问题 I'm playing with React (@13.3) with babel and webpack. I have a component that's defined like this: import BaseComponent from './BaseComponent'; export default class SomeComponent extends BaseComponent { render() { return ( <div> <img src="http://placekitten.com/g/900/600"/> </div> ); } } But I get the following error: Uncaught ReferenceError: React is not defined I understand the error: the JSX bit is compiled into React.createElement(...) but React isn't in the current scope since it's not

How to manually trigger click event in ReactJS?

谁说我不能喝 提交于 2019-11-28 04:37:54
How can I manually trigger a click event in ReactJS ? When a user clicks on element1, I want to automatically trigger a click on the input tag. <div className="div-margins logoContainer"> <div id="element1" className="content" onClick={this.uploadLogoIcon}> <div className="logoBlank" /> </div> <input accept="image/*" type="file" className="hide"/> </div> You could use the ref prop to acquire a reference to the underlying HTMLInputElement object through a callback, store the reference as a class property, then use that reference to later trigger a click from your event handlers using the

ReactJs: Prevent multiple times button press

房东的猫 提交于 2019-11-28 04:31:26
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.showUploadButton) { return; } this.disableUploadButton(function() { $.ajax({ [...] }); }); }, render() {

React.js: Disable button when input is empty

人走茶凉 提交于 2019-11-28 03:43:45
I'm new to React.js, sorry if the question sounds too stupid for you. I'm trying to disable a button when an input field is empty. What is the beest approach in React for this? I'm doing something like the following: <input ref="email"/> <button disabled={!this.refs.email}>Let me in</button> Is this correct? It's not just duplication of dynamic attribute, because I'm also curious about transferring/checking the data from one element to another. You'll need to keep the current value of the input in state (or pass changes in its value up to a parent via a callback function , or sideways , or