FAQ Database Discussion Community


React issue with updating state during re-render

javascript,reactjs
I have 2 components, a StaticComponent and an InteractiveComponent. The StaticComponent shows the user's information. It has a link to edit the information. That link has an onClick that fires off the handleEditClick function. This replaces the StaticComponent with an InteractiveComponent which has a form. var StaticComponent = React.createClass({ handleEditClick:...

ReactJS localhost Ajax call: No 'Access-Control-Allow-Origin' header

javascript,ajax,node.js,localhost,reactjs
Working on the ReactJS tutorial I spinned up a local server with >npm install -g http-server >http-server -c-1 And got local server working fine located at http://localhost:8080 Though, when I attempted to use an AJAX call within one of my components, I got the following error in my chrome console:...

how to render through a simple for-loop in Reactjs?

javascript,reactjs,flux
I have a simple react component defined below React.createClass({ generateContent:function(){ for(i = 1;i<=100;i++) if(i%2 == 0) { return <li>Even!</li> } else{ return <li>Odd!</li> } }, render:function(){ return (<ul> {this.generateContent()} </ul>) } }) While Rendering, The component returns the first statement of the for loop(<li>Odd</li>) and then exits. I'd like something...

React - superagent request per mapped item

javascript,reactjs,superagent
I would like to fire off a superagent request for each item mapped in my search results. To check if the item id is in an array: I have these methods; one to map the results, and the other a superagent fetch: renderResultNodes: function () { if(!this.props.results) return; return this.props.results.map(function...

Running into problems using React Datepicker with rails

javascript,ruby-on-rails,datepicker,reactjs
I am using this datepicker plugin for react: https://github.com/Hacker0x01/react-datepicker I keep getting the following error: Uncaught TypeError: this.props.moment is not a function in react-datepicker.min.js. I just copied the example code, and I can see the input field, but whenever I click on it to render the calendar, it keeps giving...

jsx command not found on mac terminal

node.js,npm,reactjs,jsx
Problem: I execute the following command from the macintosh terminal: $ jsx --watch src/ build/ I recieve the following output error from the terminal: -bash: jsx: command not found Relevant information: I am following the following tutorial: https://facebook.github.io/react/docs/getting-started.html I executed the following command from the tutorial with positive output: $...

hooking up the data model in ReactJS - syntax

reactjs
I'm looking off of the reactjs tutorial on how to build a comment box with react. I get that we are mapping over data, but I cant understand this syntax and part of the code. Would be really helpful if you could explain exactly what is going on here. Specifically,...

Error: Cannot resolve module 'module' in path/…/rewire/lib

reactjs,flux
I am using rewire prior to require method in my React/Flux app, to load components. But after installation I got this error: ERROR in ./~/rewire/lib/rewire.js Module not found: Error: Cannot resolve module 'module' in /Users/path/node_modules/rewire/lib @ ./~/rewire/lib/rewire.js 1:13-30 ERROR in ./~/rewire/lib/rewire.js Module not found: Error: Cannot resolve module 'fs' in...

Dynamic className in cjsx

coffeescript,reactjs
I am trying to hide a component and update the image in a button when I click said button. I have managed to hide the component by doing the follow: {<MyAwesomeComponent /> unless @state.hide} And I change the state by doing: toggleComponent = -> @setState hide: [email protected] . . ....

Canvas element does not resize when using ReactJS and PaperJS together

javascript,html,canvas,reactjs,paperjs
I have found that canvas elements correctly resize on window resize when they are inside react components. They also resize correctly when used with PaperJS. However, they do not resize when used with PaperJS and ReactJS together. Is this an incompatibility between PaperJS and ReactJS or am I instantiating PaperJS...

How to keep only one radio button selected with React

radio-button,reactjs
Im trying to learn React by doing a small application with it. My application has two radio buttons and I would like to keep always only one of them checked, but cannot seem to figure out how to uncheck the other button when I select the other one. Instead they...

ReactJS - Custom Dropdown

reactjs
I have a drop down that has a nested ul in ECMAScript 6. Relevant code for dropdown: ...... handleItemClicked(item){ alert(item) } render(){ return( .... <UnorderedList whenItemClicked={this.handleItemClicked} /> .... ) } Relevant code for unordered list: return( <ul> .... {this.props.dropItems.map(function(item){ return (<li onClick={this.props.whenItemClicked(item)} key={item}><a>{item}</a></li>) }, this)} </ul> ) Result: There are...

reactjs Uncaught TypeError: Cannot read property 'map' of undefined

javascript,jquery,ajax,reactjs
I have used getInitialState on my BrowseWidgetBox component. Although when passing the data to my MainMenu component, the data remains empty, as if the AJAX call to the api was never run in the BrowseWidgetBox. My question then, is why is this happening? Shouldn't componentDidMount call the ajax api and...

Reactjs this.state giving Uncaught TypeError: Cannot read property 'groupsData' of null

javascript,ajax,reactjs
I am doing 2 basic ajax calls to 2 different apis in one of my ReactJs components. Although, when running the call (on urls I know for certain are working and returning data), I receive: Uncaught TypeError: Cannot read property 'groupsData' of null Here is the single component: var BrowseWidgetBox...

React animation / ReactCSSTransitionGroup

reactjs
How would you make the following link - a spinner - work with reactjs http://codepen.io/awesome/pen/znGwr I am using webpack: { test: /\.css$/, loader: "style-loader!css-loader" } I know the css is working from other settings. I have tried the following, using the same css file(copy paste into local css ). Updated:...

Hide context menu on outside click

javascript,reactjs
I have a context menu that appears when you right click inside the <div>, and it only goes away if the user click left click inside the <div> again. How do i hide it when a user click anywhere on the page? my fiddle...

Reactjs - the spread operator giving error

javascript,reactjs,react-jsx
I am trying to use the spread operator in react.js but getting the error Unexpected token ... in Chrome. I am getting an error at the line 3 in this code: var Btn=React.createClass({ render: function(){ var { className, ...other }=this.props; return (<a {...other} className={joinClasses(className,"btn")} href="#" >{this.props.children}</a>); } }); and using...

React Component with subcomponents or render HTML in parent?

reactjs
I have a question regarding React patterns. When rendering a component, is it appropriate for this component to render several sub-components, or is it ok to render some HTML in the parent component. Example: If I have a box that has a heading and a body with list of elements,...

Should every object in the store state be immutable?

javascript,reactjs,immutability,flux,immutable.js
I am learning React.js and Flux, and I understand that is better to keep the object immutable because comparing them is O(1). My situation is this: I have a store, which has a _state object, that is a map (I'm using Immutable-js). That _state has some booleans, some strings and...

Get font color of ReactJS element

css,reactjs
I have a React component, a button, and I need to set the background-color of a child element to the color of the button. I know that you're not supposed to call this.refs.myElement.getDOMNode() in the render() function, so I'm not sure how I'm supposed to lay this out. At the...

ReactJS: How to optimise top-down render flow

javascript,reactjs,scalability,reactjs-flux
Looking at Facebook's Flux TodoMVC example (https://github.com/facebook/flux/tree/master/examples/flux-todomvc/) makes me wonder how this approach scales. In the example the store is updated onBlur/Enter keypress of a new entry, but what if we wanted to update an existing entry on keypress? i.e. you type something in an input and this is persisted...

How to test interactivity of React component using D3 select()?

javascript,svg,d3.js,reactjs
I have a React visualization component (Using SVG) that displays a tooltip on mouseOver, using the d3.select() function to modify a class on the tooltip element so that it becomes visible (And invisible on mouseOut): _handleMouseOverEvent: function() { d3.select('#' + this.props.caption).classed('hidden', false); d3.select('#' + this.props.caption).classed('visible', true); } _handleMouseOutEvent: function() {...

Unable to render second react component

reactjs
I am just fiddling with react to make a simple website. So here's the code <html> <head> <script src="https://fb.me/react-0.13.3.js"></script> <!-- In-browser JSX transformer, remove when pre-compiling JSX. --> <script src="https://fb.me/JSXTransformer-0.13.3.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> </head> <body> <div class="container"></div> </body> <script type="text/jsx"> var...

Page update when clicking a button (reactjs)

javascript,reactjs
Suppose I want to render page again, after clicking a button. How can I accomplish it? In particular how would you change my program to generate random texts, when clicking the button? var colors = ['red', 'yellow', 'blue', 'green']; var Hello = React.createClass({ randomText(color) { var style = { color:...

React components as plain JS objects?

javascript,class,reactjs,javascript-objects,ecmascript-6
Does anybody has experience in working with React components as plain JS objects instead of annoying ES6 classes and deprecated .createClass method. Maybe you have some examples of factory functions or similar to share? Thanks!...

React.js - Setting initial state and defining props which utilize that state within a component

reactjs
I have read: ReactJs: How to pass the initial state while rendering a component? ReactJS: Why is passing the component initial state a prop an anti-pattern? Am still having trouble resolving this issue. Namely - rendering an array of child component declarations with props that rely on the initial state...

Django Rest Framework + React + Reflux: Can't GET new objects

django,reactjs,django-rest-framework,refluxjs
I'm trying to post a new object via a React form using a Reflux action. That part is working fine. The object is posting as expected, however, when I try to GET that object programmatically, it doesn't seem to be accessible unless I log out, restart my local server, or...

How do I load SVGs directly in my React component using webpack?

svg,icons,reactjs,loader,webpack
I would like to render a material design icon directly in my NextButton component using webpack. Here's the relevant part of my code: var NextIcon = require('material-design-icons/navigation/svg/production/ic_chevron_right_18px.svg'); var NextButton = React.createClass({ render: function() { return ( <Link to={this.props.target} className='button--next'> {this.props.label} {NextIcon} </Link> ) } }); But this isn't working as...

Better/faster way for changing React array state than cloning it?

reactjs
Whenever a React class has a state object that is or contains an array, updating that state feels awkward. Usually what I do is var newArrayThing = _.clone(this.state.arrayThing); //or slice() newArrayThing[123] = 42; //update stuff this.setState({arrayThing: newArrayThing}); Is there a better or more elegant way of handling this I am...

ReactJs tries to attach onclick handler before Ajax loads element

javascript,ajax,reactjs
I am creating a dynamic list for a menu in ReactJs. The data for each menu item is bing pulled in from a JSON file via AJAX. I want the menu to close when a menu item is clicked. I am giving the whole list the prop "whenClicked" when one...

Passing an element vs. a react class to an optionally displayed modal

reactjs,react-bootstrap
I have a table cell that the user can click on which will launch a react-bootstrap modal that displays additional details. The modal displays a component that has its own state and may trigger an action to pull data from the back-end if the store doesn't have the data it...

Why should addChangeListener be in componentDidMount instead of componentWillMount?

javascript,reactjs,reactjs-flux,flux
I saw this line as an answer to another question on here: "componentWillMount should be componentDidMount, or else you'll leak event emitters in node." and I don't really understand it. Can someone explain with more detail? More info: Building a react application with flux, as part of the initial render,...

ReactJs - Using Mixins with ES6

reactjs
Following up on Egghead.io's video - 'React Components in ES 6 classes', the below works: 'use strict'; import React from 'react'; import Button from './components/form/button'; import Label from './components/form/label'; // after refactoring //import ReactMixin from './super-classes/react-mixin'; //ToDo: Reactor below let reactMixin = InnerComponent => class extends React.Component { constructor() {...

Display a div at the position of mouse click

javascript,reactjs
I have a table of cells, and I want to be able to create a menu on the point of right click. I have an example of my fiddle here: jsFiddle Now the ContextMenu class is just a div that I created for this example. As you can see there...

ReactJS Updating Array

javascript,reactjs
I'm currently starting to learn ReactJS and have run into some trouble updating an array. Currently I have a searchable list of contacts which is working fine. But using the input box below the list I am trying to add additional items to the list using the update() function. Sample...

Adding resolver to React-router

reactjs,react-router,refluxjs
In react-router, is there any way to pass a property from the Route-definition that can be picked up within the Router.run function? I want specific actions to fire for specific routes. Something like this perhaps: <Route handler={someComponent} resolve={someAction} /> In Router.Run i want to execute that given action defined in...

What is the proper way to get the value of a React