,Algorithmic
45846896,1,0,2700673,0,10,1,0,2017-08-23T18:22:14.960,1,117,Reactjs and using Redux for form handling,"I am fairly new to redux and I am trying to re-architect an old application -- so the first page will present the user with a possible splash screen and then login form -- and on successful login - present the user with the main website.
_x000D__x000D_
_x000D__x000D_
I would like some help with react antd form and redux handling/processing for login authentication handling. Enhancing these demos. Links to similar issues - something broken down etc.. common problems that may be faced etc.. - maybe even logging back out functions.
_x000D__x000D_
_x000D__x000D_
I have these old components so this is how the application used to look. I would most likely use antd for the form components.
_x000D__x000D_
_x000D__x000D_
here is a demo I've got going which uses redux -- but this just fetches data from an api -- then renders it -- I am keen to pickle parts of this code to replace parts of form handling etc..
_x000D__x000D_
_x000D__x000D_
this redux demo is on this jsfiddle for reference.
_x000D__x000D_
_x000D__x000D_
http://jsfiddle.net/0ht35rpb/104/
_x000D__x000D_
_x000D__x000D_
//redux demo
_x000D__x000D_
_x000D__x000D_
import React, { Component } from 'react'_x000D__x000D_
import { render } from 'react-dom'_x000D__x000D_
import {Provider, connect} from 'react-redux'_x000D__x000D_
import {createStore, applyMiddleware} from 'redux' _x000D__x000D_
import thunk from 'redux-thunk';_x000D__x000D_
_x000D__x000D_
import MapChart from './modules/mapChart/MapChart'_x000D__x000D_
import './index.css'_x000D__x000D_
_x000D__x000D_
function fetchPostsRequest(){_x000D__x000D_
return {_x000D__x000D_
type: ""FETCH_REQUEST""_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
function fetchPostsSuccess(payload) {_x000D__x000D_
return {_x000D__x000D_
type: ""FETCH_SUCCESS"",_x000D__x000D_
payload_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
function fetchPostsError() {_x000D__x000D_
return {_x000D__x000D_
type: ""FETCH_ERROR""_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
const reducer = (state = {}, action) => {_x000D__x000D_
switch (action.type) {_x000D__x000D_
case ""FETCH_REQUEST"":_x000D__x000D_
return state;_x000D__x000D_
case ""FETCH_SUCCESS"": _x000D__x000D_
return {...state, posts: action.payload};_x000D__x000D_
default:_x000D__x000D_
return state;_x000D__x000D_
}_x000D__x000D_
} _x000D__x000D_
_x000D__x000D_
function fetchPostsWithRedux() {_x000D__x000D_
return (dispatch) => {_x000D__x000D_
dispatch(fetchPostsRequest());_x000D__x000D_
return fetchPosts().then(([response, json]) =>{_x000D__x000D_
if(response.status === 200){_x000D__x000D_
dispatch(fetchPostsSuccess(json))_x000D__x000D_
}_x000D__x000D_
else{_x000D__x000D_
dispatch(fetchPostsError())_x000D__x000D_
}_x000D__x000D_
})_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
function fetchPosts() {_x000D__x000D_
const URL = 'https://data.police.uk/api/crimes-street/all-crime?poly=52.268,0.543:52.794,0.238:52.130,0.478&date=2017-01';_x000D__x000D_
return fetch(URL, { method: 'GET'})_x000D__x000D_
.then( response => Promise.all([response, response.json()]));_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
// this is how you'll get your icon links_x000D__x000D_
// instead of a switch with loads of repetitive bytes_x000D__x000D_
const iconMap = {_x000D__x000D_
'anti-social-behaviour': 'green-dot',_x000D__x000D_
'burglary': 'red-dot',_x000D__x000D_
'other-theft': 'pink-dot',_x000D__x000D_
'public-order': 'purple',_x000D__x000D_
'robbery': 'yellow',_x000D__x000D_
'vehicle-crime': 'orange',_x000D__x000D_
'violent-crime': 'orange-dot',_x000D__x000D_
'other-crime': 'ltblue-dot',_x000D__x000D_
'criminal-damage-arson': 'yellow-dot',_x000D__x000D_
'drugs': 'purple-dot',_x000D__x000D_
'shoplifting': 'blue-dot'_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
// this is a class because it needs state_x000D__x000D_
class CrimeMap extends Component {_x000D__x000D_
state = {_x000D__x000D_
markers: []_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
componentDidMount() { _x000D__x000D_
this.props.fetchPostsWithRedux()_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
componentWillReceiveProps(nextProps){_x000D__x000D_
this.setState({markers: this.mapLayerData(nextProps.posts)});_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
// store only the data you want to pass as props to each Marker_x000D__x000D_
// instead of mapping it directly in MapChart every time it renders_x000D__x000D_
mapLayerData(markers) {_x000D__x000D_
// use a standard map function instead of $.each_x000D__x000D_
// destructuring saves time and repetition_x000D__x000D_
return markers.map(({ category, location }) => ({_x000D__x000D_
// use a template string and a simple map of icon names to get your icon uri_x000D__x000D_
icon: 'http://maps.google.com/mapfiles/ms/icons/'+ iconMap[category] +'.png',_x000D__x000D_
label: category,_x000D__x000D_
name: category,_x000D__x000D_
position: {_x000D__x000D_
lat: location.latitude,_x000D__x000D_
lng: location.longitude_x000D__x000D_
}_x000D__x000D_
}))_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
render() {_x000D__x000D_
// there's only one layer, so render it directly_x000D__x000D_
return (_x000D__x000D_
<div className=""app"">_x000D__x000D_
<MapChart markers={this.state.markers} />_x000D__x000D_
</div>_x000D__x000D_
)_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
function mapStateToProps(state){_x000D__x000D_
return {_x000D__x000D_
posts: state.posts_x000D__x000D_
}_x000D__x000D_
} _x000D__x000D_
_x000D__x000D_
let Container = connect(mapStateToProps, {fetchPostsWithRedux})(CrimeMap);_x000D__x000D_
_x000D__x000D_
const store = createStore(_x000D__x000D_
reducer,_x000D__x000D_
applyMiddleware(thunk)_x000D__x000D_
);_x000D__x000D_
_x000D__x000D_
render(_x000D__x000D_
<Provider store={store}>_x000D__x000D_
<Container/>_x000D__x000D_
</Provider>,_x000D__x000D_
document.getElementById('root')_x000D__x000D_
);_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
here is the jsfiddle with the old code on - if its beneficial to demonstrate changes/improvements there.
_x000D__x000D_
_x000D__x000D_
http://jsfiddle.net/0ht35rpb/103/
_x000D__x000D_
_x000D__x000D_
//Old code
_x000D__x000D_
_x000D__x000D_
import React from 'react';_x000D__x000D_
import ReactDOM from 'react-dom';_x000D__x000D_
_x000D__x000D_
//splash page_x000D__x000D_
var SplashPage = React.createClass({_x000D__x000D_
componentDidMount: function() {_x000D__x000D_
_x000D__x000D_
function animateSplash(){_x000D__x000D_
$('.splash-page').animate({_x000D__x000D_
opacity: 0,_x000D__x000D_
}, 2500, ""linear"", function() {_x000D__x000D_
// Animation complete._x000D__x000D_
$('.splash-page').hide();_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
setTimeout(function(){ _x000D__x000D_
animateSplash();_x000D__x000D_
}, 4000);_x000D__x000D_
_x000D__x000D_
},_x000D__x000D_
render: function() {_x000D__x000D_
return (_x000D__x000D_
<div className=""splash-page"">_x000D__x000D_
<div className=""content"">_x000D__x000D_
<div className=""splash"">_x000D__x000D_
SPLASH PAGE_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
);_x000D__x000D_
}_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
//form page_x000D__x000D_
var LandingForm = React.createClass({_x000D__x000D_
componentDidMount: function(){_x000D__x000D_
_x000D__x000D_
},_x000D__x000D_
getInitialState: function() {_x000D__x000D_
return {postcode: '', propertytype: '', propertysize: '', propertytypename: '', isValid: false};_x000D__x000D_
},_x000D__x000D_
checkPostCodeValid: function(target, postcode){_x000D__x000D_
_x000D__x000D_
var host = this.props.source[0][""local""];_x000D__x000D_
if(window.location.hostname != ""localhost""){_x000D__x000D_
host = this.props.source[0][""live""];_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
var isPostcodeValidApiCall = host + 'codeigniter/index.php/api/isPostcodeValid/'+postcode;_x000D__x000D_
_x000D__x000D_
//quick hack to bypass email validator on computer with no db_x000D__x000D_
if(window.location.hostname == ""localhost""){_x000D__x000D_
isPostcodeValidApiCall = host + ""_test_emailvalidate_json1_nw1.php"";_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
var that = this;_x000D__x000D_
_x000D__x000D_
this.serverRequest = $.get(isPostcodeValidApiCall, function (response) {_x000D__x000D_
var response = JSON.parse(response);_x000D__x000D_
var errorMsg = response.isPostcodeValid.errorMessage;_x000D__x000D_
var isValid = response.isPostcodeValid.isValid;_x000D__x000D_
_x000D__x000D_
if(!isValid){_x000D__x000D_
//show error message_x000D__x000D_
setLabelError(target, errorMsg, true);//target, error message, show error_x000D__x000D_
}_x000D__x000D_
else{_x000D__x000D_
//do not show error_x000D__x000D_
setLabelError(target, errorMsg, false);//target, error message, show error_x000D__x000D_
} _x000D__x000D_
_x000D__x000D_
that.setState({_x000D__x000D_
isValid: isValid_x000D__x000D_
});_x000D__x000D_
}.bind(this));_x000D__x000D_
_x000D__x000D_
},_x000D__x000D_
handlePostcodeChange: function(e) {_x000D__x000D_
this.setState({postcode: e.target.value}); _x000D__x000D_
},_x000D__x000D_
handleValidation: function(e){_x000D__x000D_
//do a check on char lengths greater than 1_x000D__x000D_
if((e.target.value).length > 1){_x000D__x000D_
this.checkPostCodeValid(e.target, e.target.value);_x000D__x000D_
}_x000D__x000D_
},_x000D__x000D_
handlePropertyTypeChange: function(val) {_x000D__x000D_
this.setState({propertytypename: val.label});_x000D__x000D_
this.setState({propertytype: val.value});_x000D__x000D_
setReactSelectorLabel(val);_x000D__x000D_
},_x000D__x000D_
handlePropertySizeChange: function(val) {_x000D__x000D_
this.setState({propertysize: val.value});_x000D__x000D_
setReactSelectorLabel(val);_x000D__x000D_
},_x000D__x000D_
handleSubmit: function(e) {_x000D__x000D_
e.preventDefault();_x000D__x000D_
var postcode = this.state.postcode.trim().toUpperCase();_x000D__x000D_
var propertytype = this.state.propertytype.trim();_x000D__x000D_
var propertysize = this.state.propertysize.trim();_x000D__x000D_
var isValid = this.state.isValid;_x000D__x000D_
//console.log(""isValid"", isValid);_x000D__x000D_
_x000D__x000D_
var propertytypename = this.state.propertytypename.trim();_x000D__x000D_
if (!postcode || !propertytype || !propertysize || !isValid) {_x000D__x000D_
return;_x000D__x000D_
}_x000D__x000D_
// TODO: send request to the server_x000D__x000D_
_x000D__x000D_
this.setState({postcode: '', propertytype: '', propertysize: '', propertytypename: '', isValid: false});_x000D__x000D_
_x000D__x000D_
var shortPostcode = ((postcode.substring(0, postcode.length-3)).trim());_x000D__x000D_
var postCodeSector = ((postcode.substring(0, postcode.length-2)).trim());_x000D__x000D_
_x000D__x000D_
//build queries_x000D__x000D_
var masterApiCall = '/api/master/'+shortPostcode+'/'+propertytype+'/'+propertysize;_x000D__x000D_
var directApiCall = '/api/direct/'+postcode;_x000D__x000D_
_x000D__x000D_
var dataEntry = [_x000D__x000D_
{_x000D__x000D_
""directApiCall"" : directApiCall,_x000D__x000D_
""masterApiCall"": masterApiCall,_x000D__x000D_
""postCodeSector"": postCodeSector,_x000D__x000D_
""postCode"": shortPostcode,_x000D__x000D_
""fullPostCode"": postcode,_x000D__x000D_
""propertyType"": propertytype,_x000D__x000D_
""propertySize"": propertysize,_x000D__x000D_
""propertyTypeName"": propertytypename_x000D__x000D_
}_x000D__x000D_
];_x000D__x000D_
_x000D__x000D_
ReactDOM.render(_x000D__x000D_
<DataPage root={initConfig} source={dataEntry} />,_x000D__x000D_
document.getElementById('root'),_x000D__x000D_
function() {_x000D__x000D_
//xx_x000D__x000D_
}_x000D__x000D_
);_x000D__x000D_
_x000D__x000D_
},_x000D__x000D_
componentWillUnmount: function() {_x000D__x000D_
this.serverRequest.abort();_x000D__x000D_
},_x000D__x000D_
render: function() {_x000D__x000D_
_x000D__x000D_
var optionsPropertyType = [_x000D__x000D_
{ value: 'val1', label: 'key1' },_x000D__x000D_
{ value: 'val2', label:'key2'}_x000D__x000D_
]; _x000D__x000D_
_x000D__x000D_
var optionsPropertySize = [_x000D__x000D_
{ value: 'val1', label: 'key1' },_x000D__x000D_
{ value: 'val2', label:'key2'}_x000D__x000D_
];_x000D__x000D_
_x000D__x000D_
return (_x000D__x000D_
_x000D__x000D_
<div className=""container"" data-role=""screenbackground"">_x000D__x000D_
_x000D__x000D_
<div className=""row"">_x000D__x000D_
_x000D__x000D_
<div className=""col-md-12 col"">_x000D__x000D_
_x000D__x000D_
<div className=""form-components""> _x000D__x000D_
_x000D__x000D_
<form id=""mainform"" data-role=""form-aesethetics"" className=""commentForm"" onSubmit={this.handleSubmit}>_x000D__x000D_
<fieldset>_x000D__x000D_
<label>Postcode</label>_x000D__x000D_
<input _x000D__x000D_
placeholder=""Postcode""_x000D__x000D_
type=""text"" _x000D__x000D_
name=""postcode""_x000D__x000D_
value={this.state.postcode}_x000D__x000D_
onChange={this.handlePostcodeChange}_x000D__x000D_
onKeyPress={this.handleValidation}_x000D__x000D_
/>_x000D__x000D_
</fieldset>_x000D__x000D_
_x000D__x000D_
<fieldset>_x000D__x000D_
<label>Property Type</label>_x000D__x000D_
_x000D__x000D_
<Select_x000D__x000D_
name=""propertytype""_x000D__x000D_
value={this.state.propertytype}_x000D__x000D_
options= {optionsPropertyType}_x000D__x000D_
onChange={this.handlePropertyTypeChange}_x000D__x000D_
placeholder=""Property type""_x000D__x000D_
/>_x000D__x000D_
_x000D__x000D_
</fieldset>_x000D__x000D_
_x000D__x000D_
<fieldset>_x000D__x000D_
<label>Property Size</label>_x000D__x000D_
_x000D__x000D_
<Select_x000D__x000D_
name=""propertysize""_x000D__x000D_
value={this.state.propertysize}_x000D__x000D_
options= {optionsPropertySize}_x000D__x000D_
onChange={this.handlePropertySizeChange}_x000D__x000D_
placeholder=""Property size""_x000D__x000D_
/>_x000D__x000D_
_x000D__x000D_
</fieldset>_x000D__x000D_
_x000D__x000D_
<fieldset className=""aligninmobile"">_x000D__x000D_
<input type=""submit"" disabled={!this.state.isValid} value=""Let's Go""/>_x000D__x000D_
</fieldset>_x000D__x000D_
</form>_x000D__x000D_
</div>_x000D__x000D_
_x000D__x000D_
</div>_x000D__x000D_
_x000D__x000D_
</div>_x000D__x000D_
_x000D__x000D_
</div>_x000D__x000D_
_x000D__x000D_
);_x000D__x000D_
}_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
//Data Page_x000D__x000D_
var DataPage = React.createClass({_x000D__x000D_
_x000D__x000D_
getInitialState: function() {_x000D__x000D_
return {_x000D__x000D_
rawDirectData: ''_x000D__x000D_
};_x000D__x000D_
},_x000D__x000D_
_x000D__x000D_
componentDidMount: function () {_x000D__x000D_
//check host to see which source to use_x000D__x000D_
var host = this.props.root[0][""local""];_x000D__x000D_
if(window.location.hostname != ""localhost""){_x000D__x000D_
host = this.props.root[0][""live""];_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
var directApi = host + this.props.source[0][""directApiCall""];_x000D__x000D_
_x000D__x000D_
if(window.location.hostname == ""localhost""){_x000D__x000D_
directApi = host + ""_test_direct_json1_nw1.php"";_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
var that = this;_x000D__x000D_
function requestData(source, callback){_x000D__x000D_
that.serverRequest = $.get(source, function (response) {_x000D__x000D_
callback(response);_x000D__x000D_
}.bind(that)); _x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
requestData(directApi, function(rawDirectData){_x000D__x000D_
that.setState({_x000D__x000D_
rawDirectData: JSON.parse(rawDirectData)_x000D__x000D_
}); _x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
},_x000D__x000D_
_x000D__x000D_
componentWillUnmount: function() {_x000D__x000D_
this.serverRequest.abort();_x000D__x000D_
},_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
render: function() {_x000D__x000D_
var props = this.props;_x000D__x000D_
_x000D__x000D_
var directApiData = this.state.rawDirectData;_x000D__x000D_
_x000D__x000D_
return (_x000D__x000D_
<div className=""container"">_x000D__x000D_
<div className=""row"">_x000D__x000D_
<div className=""col-md-12 col"">_x000D__x000D_
<MultipleComponents root={props.root} source={props.source} />_x000D__x000D_
</div>_x000D__x000D_
<div className=""col-md-12 col"">_x000D__x000D_
<ContactForm root={props.root} source={props.source} data={directApiData}/>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
);_x000D__x000D_
}_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
//LandingForm_x000D__x000D_
ReactDOM.render(_x000D__x000D_
<div>_x000D__x000D_
<SplashPage />_x000D__x000D_
<LandingForm source={initConfig} />_x000D__x000D_
</div>,_x000D__x000D_
document.getElementById('root'),_x000D__x000D_
function(){ _x000D__x000D_
}_x000D__x000D_
);_x000D__x000D_
_x000D__x000D_
",,Algorithmic
45991500,1,0,202294,1,1,0,0,2017-08-31T23:37:30.703,2,2344,MobX observable with dynamic data,"I have the following class
_x000D__x000D_
_x000D__x000D_
export default class BaseStore {_x000D__x000D_
@observable model ;_x000D__x000D_
_x000D__x000D_
@action updateStore(propertyName, newValue) {_x000D__x000D_
this.model[propertyName] = newValue;_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
In child classes I add layers to the observable model, such as :
_x000D__x000D_
_x000D__x000D_
model.payment.type = 'credit card'_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
My react component doesn't render automatically when this happen, it does however, if I has a top level data such as:
_x000D__x000D_
_x000D__x000D_
model.Type = 'CreditCard'_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
I am new to MobX, and read that I need to make use of map() but I am unable to find a decent example that explain how to use it.
_x000D__x000D_
",,Algorithmic
46134524,1,0,7697399,0,0,0,0,2017-09-09T19:46:24.457,1,209,Deploying NodeJS React project on LAMP server,"I have been developing a web application (as part of a project in my institute) using NodeJS, MongoDB (Mongoose) and React. In the architecture I have an API server made using NodeJS and the database is stored locally in my machine. Also, I have a React project (independent of the NodeJS server). I am using APIs to get information from the node server into React application. My NodeJS app is deployed at localhost:4000 and react website on localhost:3000 Now I have to push this project to production.
_x000D__x000D_
_x000D__x000D_
I have been given a server to SSH into and deploy the website. The server is an Apache server and in its root a WordPress site has been installed. I have been asked to deploy the website at the URL Somedomain.com/ProjectName . With my current understand of the state, I would have to create a subfolder with the Project's Name and serve the site content from there. Though I have deployed NodeJS apps remotely on NodeJS hosts, I have no experience with above cases.
_x000D__x000D_
_x000D__x000D_
How should I proceed with this? What would be the fastest approach to tackle this as I have a time constraint.
_x000D__x000D_
",,Algorithmic
46286286,1,0,8628357,2,0,0,0,2017-09-18T18:52:21.203,2,323,What is the reason to use redux on react?,"Have anybody a good reason to use redux on react? I was thinking to build very small components in react, so i don't need redux._x000D__x000D_
Every component has his own state like redux.
_x000D__x000D_
_x000D__x000D_
What do you think? What kind of experience do you have?
_x000D__x000D_
",,Other
46447713,1,46448550,6947248,2,0,1,0,2017-09-27T12:19:37.303,16,35157,React-Table column fixed width spoils whole table,"Can someone explain this to me why i cant set fixed width on First Name column?It's setting passed width but it spoils whole table.If you tinker with resizing(by mouse) the column other columns will appear.Is this due to flebox?Im not familiar with flexbox.
_x000D__x000D_
_x000D__x000D_
_x000D__x000D__x000D_
_x000D__x000D__x000D_
var ReactTable = ReactTable.default_x000D__x000D__x000D_
class App extends React.Component {_x000D__x000D__x000D_
constructor() {_x000D__x000D__x000D_
super();_x000D__x000D__x000D_
this.state = {_x000D__x000D__x000D_
data: []_x000D__x000D__x000D_
};_x000D__x000D__x000D_
}_x000D__x000D__x000D_
render() {_x000D__x000D__x000D_
const { data } = this.state;_x000D__x000D__x000D_
return (_x000D__x000D__x000D_
<div>_x000D__x000D__x000D_
<ReactTable_x000D__x000D__x000D_
data={data}_x000D__x000D__x000D_
columns={[_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Name"",_x000D__x000D__x000D_
columns: [_x000D__x000D__x000D_
{_x000D__x000D__x000D_
width:'50',_x000D__x000D__x000D_
Header: ""First Name"",_x000D__x000D__x000D_
accessor: ""firstName""_x000D__x000D__x000D_
},_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Last Name"",_x000D__x000D__x000D_
id: ""lastName"",_x000D__x000D__x000D_
accessor: d => d.lastName_x000D__x000D__x000D_
}_x000D__x000D__x000D_
]_x000D__x000D__x000D_
},_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Info"",_x000D__x000D__x000D_
columns: [_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Age"",_x000D__x000D__x000D_
accessor: ""age""_x000D__x000D__x000D_
},_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Status"",_x000D__x000D__x000D_
accessor: ""status""_x000D__x000D__x000D_
}_x000D__x000D__x000D_
]_x000D__x000D__x000D_
},_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: 'Stats',_x000D__x000D__x000D_
columns: [_x000D__x000D__x000D_
{_x000D__x000D__x000D_
Header: ""Visits"",_x000D__x000D__x000D_
accessor: ""visits""_x000D__x000D__x000D_
}_x000D__x000D__x000D_
]_x000D__x000D__x000D_
}_x000D__x000D__x000D_
]}_x000D__x000D__x000D_
defaultPageSize={10}_x000D__x000D__x000D_
className=""-striped -highlight""_x000D__x000D__x000D_
/>_x000D__x000D__x000D_
<br />_x000D__x000D__x000D_
_x000D__x000D__x000D_
</div>_x000D__x000D__x000D_
);_x000D__x000D__x000D_
}_x000D__x000D__x000D_
}_x000D__x000D__x000D_
_x000D__x000D__x000D_
ReactDOM.render(<App />, document.getElementById(""app""));
_x000D__x000D__x000D_
<link href=""https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.css"" rel=""stylesheet""/>_x000D__x000D__x000D_
_x000D__x000D__x000D_
<script src=""https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.js""></script>_x000D__x000D__x000D_
<script src=""https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js""></script>_x000D__x000D__x000D_
<script src=""https://cdnjs.cloudflare.com/ajax/libs/react-table/6.5.3/react-table.js""></script>_x000D__x000D__x000D_
_x000D__x000D__x000D_
<div id=""app""></div>
_x000D__x000D__x000D_
_x000D__x000D__x000D_
_x000D__x000D__x000D_
_x000D__x000D_
_x000D__x000D_
If anyone can add react-table
to the tags i will be glad.
_x000D__x000D_
",,Misconception
46590228,1,46590868,5539574,2,1,0,0,2017-10-05T16:07:16.657,2,3405,Spring Boot and React can't load index.html,"I'm trying to create a simple web application using create-react-app
and Spring Boot, but spring can't find index.html
in resources.
_x000D__x000D_
_x000D__x000D_
React's build
folder is copied to target
by maven-resources-plugin
:
_x000D__x000D_
_x000D__x000D_
<plugin>_x000D__x000D_
<artifactId>maven-resources-plugin</artifactId>_x000D__x000D_
..._x000D__x000D_
<phase>package</phase>_x000D__x000D_
<goals>_x000D__x000D_
<goal>copy-resources</goal>_x000D__x000D_
</goals>_x000D__x000D_
..._x000D__x000D_
<outputDirectory>${basedir}/target/classes</outputDirectory>_x000D__x000D_
<resources>_x000D__x000D_
<resource>_x000D__x000D_
<directory>src/main/app/build</directory>_x000D__x000D_
<filtering>true</filtering>_x000D__x000D_
</resource>_x000D__x000D_
</resources>_x000D__x000D_
... _x000D__x000D_
</plugin>_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
This is my project structure:
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Controller:
_x000D__x000D_
_x000D__x000D_
@Controller_x000D__x000D_
public class BasicController {_x000D__x000D_
@RequestMapping(value = ""/"", method = RequestMethod.GET)_x000D__x000D_
public String index() {_x000D__x000D_
return ""index"";_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Get request to localhost:8080
returns 404
. Could you please point me where am i mistaken.
_x000D__x000D_
_x000D__x000D_
UPDATE:
_x000D__x000D_
_x000D__x000D_
Managed to make it working by changing React's build output directory in maven plugin to_x000D__x000D_
${basedir}/src/main/resources/META-INF/resources
_x000D__x000D_
and return ""index""
to return index.html
.
_x000D__x000D_
",,Algorithmic
46724723,1,46725956,8724053,1,0,0,0,2017-10-13T07:30:27.093,2,3870,How to store Cookies for the next session in ReactJS for Sign-Up functionalities. Can anyone give me some idea?,"How to store Cookies for the next session in ReactJS for Sign-In functionalities. Can anyone give me some idea? Following is my ReactJs code for Sign-In page. Basically what I am trying to do is, a user can click the remember me box and that data should get store for the next time. Each time user don't have to enter the details after clicking the Remember Me box.
_x000D__x000D_
_x000D__x000D_
import React, {Component} from ""react"";_x000D__x000D_
import axios from 'axios';_x000D__x000D_
import {BrowserRouter as Router, Route, Link} from 'react-router-dom';_x000D__x000D_
import {SignUp} from ""./SignUp"";_x000D__x000D_
import {ForgotPassword} from ""./ForgotPassword"";_x000D__x000D_
_x000D__x000D_
export class SignIn extends React.Component {_x000D__x000D_
constructor(props) {_x000D__x000D_
super(props);_x000D__x000D_
this.state = {_x000D__x000D_
fields: {},_x000D__x000D_
errors: {},_x000D__x000D_
remember_me: false_x000D__x000D_
}_x000D__x000D_
this.onSubmit = this.onSubmit.bind(this);_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
handleValidation(){_x000D__x000D_
let fields = this.state.fields;_x000D__x000D_
let errors = {};_x000D__x000D_
let formIsValid = true;_x000D__x000D_
_x000D__x000D_
//Email_x000D__x000D_
if(!fields[""email""]){_x000D__x000D_
formIsValid = false;_x000D__x000D_
errors[""email""] = ""Email ID is required"";_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
//Password_x000D__x000D_
if(!fields[""password""]){_x000D__x000D_
formIsValid = false;_x000D__x000D_
errors[""password""] = ""Password is required"";_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
this.setState({errors: errors});_x000D__x000D_
return formIsValid;_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
onSubmit(e){_x000D__x000D_
e.preventDefault();_x000D__x000D_
if(this.state.fields[""remember_me""]){_x000D__x000D_
var Cookies = this.state.fields;_x000D__x000D_
console.log(Cookies);_x000D__x000D_
}_x000D__x000D_
if(this.handleValidation()){_x000D__x000D_
var apiBaseUrl = ""http://rediy-dev.railsfactory.com/"";_x000D__x000D_
var input = this.state.fields;_x000D__x000D_
axios.post(apiBaseUrl+'/api/v1/sessions/login', input)_x000D__x000D_
.then(function (response) {_x000D__x000D_
console.log(response);_x000D__x000D_
if(response.data.status == 200){_x000D__x000D_
console.log(""Login successful"");_x000D__x000D_
alert(""Login successful"");_x000D__x000D_
}_x000D__x000D_
else if(response.data.status == 422){_x000D__x000D_
console.log(""Invalid user details. Please enter again"");_x000D__x000D_
alert(""Invalid user details. Please enter again"");_x000D__x000D_
}_x000D__x000D_
})_x000D__x000D_
.catch(function (error) {_x000D__x000D_
console.log(error);_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
onChange(field, e){_x000D__x000D_
let fields = this.state.fields;_x000D__x000D_
fields[field] = e.target.value;_x000D__x000D_
this.setState({fields});_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
render() {_x000D__x000D_
return (_x000D__x000D_
<div id=""login"" className=""form-popup"" role=""dialog"">_x000D__x000D_
<div className=""modal-dialog"">_x000D__x000D_
<div className=""modal-content"">_x000D__x000D_
<form className=""form-theme text-left"">_x000D__x000D_
<div className=""modal-header"">_x000D__x000D_
<button type=""button"" className=""close"" data-dismiss=""modal"">×</button>_x000D__x000D_
<h1 className=""theme-title text-center"">Sign In</h1>_x000D__x000D_
</div>_x000D__x000D_
<div className=""modal-body"">_x000D__x000D_
<div className=""row"">_x000D__x000D_
<div className=""col-sm-12"">_x000D__x000D_
<div className=""form-group input"">_x000D__x000D_
<input_x000D__x000D_
value={this.state.fields[""email""]}_x000D__x000D_
onChange={this.onChange.bind(this, ""email"")}_x000D__x000D_
className=""form-control input__field""_x000D__x000D_
type=""text""_x000D__x000D_
id=""unit""_x000D__x000D_
refs=""email""_x000D__x000D_
placeholder=""Email Id *""_x000D__x000D_
/>_x000D__x000D_
<span style={{color: ""red""}}>{this.state.errors[""email""]}</span>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
<div className=""col-sm-12"">_x000D__x000D_
<div className=""form-group input"">_x000D__x000D_
<input_x000D__x000D_
value={this.state.fields[""password""]}_x000D__x000D_
onChange={this.onChange.bind(this, ""password"")}_x000D__x000D_
className=""form-control input__field""_x000D__x000D_
type=""text""_x000D__x000D_
id=""unit""_x000D__x000D_
refs=""password""_x000D__x000D_
placeholder=""Password *""_x000D__x000D_
/>_x000D__x000D_
<span style={{color: ""red""}}>{this.state.errors[""password""]}</span>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
<div className=""col-sm-6"">_x000D__x000D_
<div className=""forgot-password"">_x000D__x000D_
<p> <Link to=""/ForgotPassword"">Forgot your password</Link></p>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
<div className=""col-sm-6"">_x000D__x000D_
<div className=""register"">_x000D__x000D_
<p>New User? <Link to=""/SignUp"">Register</Link></p>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
<div className=""col-sm-12"">_x000D__x000D_
<div className=""checkbox checkbox-primary"">_x000D__x000D_
<input_x000D__x000D_
refs=""remember_me""_x000D__x000D_
value={true}_x000D__x000D_
onChange={this.onChange.bind(this, ""remember_me"")}_x000D__x000D_
id=""checkbox1""_x000D__x000D_
type=""checkbox"" />_x000D__x000D_
<label htmlFor=""checkbox1"">_x000D__x000D_
Remember Me_x000D__x000D_
</label>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
<div className=""modal-footer"">_x000D__x000D_
<div className=""btn-wrap"">_x000D__x000D_
<button type=""submit"" className=""btn btn-theme save btn btn-primary"" onClick={this.onSubmit}>Sign In</button>_x000D__x000D_
<button type=""submit"" className=""btn btn-theme btn btn-danger"" data-dismiss=""modal"">Cancel</button>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</form>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
</div>_x000D__x000D_
);_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
",,Algorithmic
46869330,1,0,8469857,1,0,0,0,2017-10-22T00:15:19.737,2,1601,Most Efficient way to Combine Arrays removing Duplicates,"Using a Flatlist and looking for an efficient way to combine elements of a list, while removing the duplicate elements. Each element has a unique key value, to know whether it is a duplicate or not.
_x000D__x000D_
_x000D__x000D_
My current implimentation uses the concat function:
_x000D__x000D_
_x000D__x000D_
Array.prototype.unique = function() {_x000D__x000D_
var a = this.concat();_x000D__x000D_
for(var i=0; i<a.length; ++i) {_x000D__x000D_
for(var j=i+1; j<a.length; ++j) {_x000D__x000D_
if(a[i].key === a[j].key)_x000D__x000D_
a.splice(j--, 1);_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
return a;_x000D__x000D_
};_x000D__x000D_
_x000D__x000D_
const OldArray = this.state.data;_x000D__x000D_
const NewArray = [] //contains values we loaded in _x000D__x000D_
const FinalArray = OldArray.concat(NewArray).unique();_x000D__x000D_
_x000D__x000D_
//Update the State_x000D__x000D_
this.setState({_x000D__x000D_
data: FinalArray_x000D__x000D_
)}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
This definitely works, but every time this function runs its at least N^2 efficiency, which seems bad. Is there a better way to do this? I feel like there must be..
_x000D__x000D_
",,Algorithmic
47020768,1,47024842,8691685,1,3,0,0,2017-10-30T17:12:02.127,0,428,React Native app sides being cut off in Android,"I have absolutely no idea what might be causing this. It's only happening on the newest version we just released of the app, so it might be a little bit easier to track down the culprit, but still I can't figure out what's going on and no google search managed to helped me out.
_x000D__x000D_
_x000D__x000D_
I'm pasting a print of what's going on. As stated before I have no idea what can be causing this, so I don't know what code to paste here, but I'll provide code as you ask for. Thanks in advance!
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
",,Misconception
47163734,1,47166163,8033705,3,9,0,0,2017-11-07T17:15:03.647,7,10766,How can I fix 'warning Expected 'this' to be used by class method ' eslint error?,"I am creating a PDF like this inside a react Component.
_x000D__x000D_
_x000D__x000D_
export class Test extends React.PureComponent {
_x000D__x000D_
_x000D__x000D_
savePDF() {_x000D__x000D_
const source = document.getElementById('printContainer');_x000D__x000D_
/* eslint new-cap: [""error"", { ""newIsCap"": false }]*/_x000D__x000D_
let pdf = new jspdf('p', 'pt', 'letter');_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
let margins = { top: 50,_x000D__x000D_
left: 60,_x000D__x000D_
width: 612_x000D__x000D_
};_x000D__x000D_
_x000D__x000D_
pdf.fromHTML(_x000D__x000D_
source, _x000D__x000D_
margins.left, _x000D__x000D_
margins.top, _x000D__x000D_
{_x000D__x000D_
width: margins.width_x000D__x000D_
},_x000D__x000D_
() => {_x000D__x000D_
pdf.save('worksheet.pdf');_x000D__x000D_
}_x000D__x000D_
);_x000D__x000D_
} _x000D__x000D_
_x000D__x000D_
_x000D__x000D_
and I am getting warning Expected 'this' to be used by class method 'savePDF' class-me
_x000D__x000D_
_x000D__x000D_
this is being called an click like this onClick={this.savePDF}
see below
_x000D__x000D_
_x000D__x000D_
render() {_x000D__x000D_
<Link_x000D__x000D_
name=""save-to-pdf""_x000D__x000D_
onClick={this.savePDF}_x000D__x000D_
button=""secondary"">_x000D__x000D_
Save to PDF</Link>_x000D__x000D_
<div id=""printContainer"" className=""cf-app-segment--alt cf-hearings-worksheet"">..._x000D__x000D_
_x000D__x000D_
",,Misinterpretation
47303474,1,0,8944043,1,0,0,0,2017-11-15T09:19:01.910,0,911,Install ad-hoc ipa,"Through my react-native app I am trying to make the user able to install another enterprise app. The link to the second app looks like this
_x000D__x000D_
_x000D__x000D_
itms-services://?action=download-manifest&url=https://example.com//667/app-667.plist?token=jhsdbafkjkwdsfqkjdwkdjqs_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
I tried to open the URL using Linking
_x000D__x000D_
_x000D__x000D_
Linking.openURL(url)_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
and also using WebView
_x000D__x000D_
_x000D__x000D_
<View>_x000D__x000D_
<WebView_x000D__x000D_
source={{uri: this.state.appUrl}}_x000D__x000D_
/>_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
But nothing happens. Have you an idea aboout the issue?_x000D__x000D_
To use Linking shall I link RCTLinking to my project? I read something about it here but I am not sure.
_x000D__x000D_
",,Misconception
47451185,1,0,5529396,2,2,0,0,2017-11-23T08:53:14.157,5,1225,Microfrontends React/Component based splitting,"Background:_x000D__x000D_
I am confronted with the task to modernize a tool and convert it to microservices with a react frontend. My idea was to have a general top-level component containing e.g. the Nav and a component for each microservice that contains the functionality.
_x000D__x000D_
_x000D__x000D_
Approaches:
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
- bundle the components locally so that it becomes effectively a monolithic frontend and the the forntend code is seperated just in the repo.
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
I think that would give up on the advantage of not having to redeploy your entire app for every change
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
- lazy-load a minified bundle of each of the components from the microservice by defining them in a config file
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
With this approach I could just webpack each component on their own and async import it from the Main Page but maybe there would be a huge overhead
_x000D__x000D_
_x000D__x000D_
I read about component based splitting with react-loadable and bundling react-router or webpack, but I can't find information on how to load small bundles form different URL-Endpoints._x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Question:_x000D__x000D_
Is it possible to bundle react components on their own and import them from different Resource-URL's and how would one approach that ?(Or is React even suitable for that)
_x000D__x000D_
",,Other
47597721,1,0,7879938,1,5,0,0,2017-12-01T16:40:26.420,2,2615,Hash Router back button with React Router v4,"I'm using HashRouter to setup my App like so:
_x000D__x000D_
_x000D__x000D_
import {HashRouter as Router, Route, Switch, Link} from 'react-router-dom';_x000D__x000D_
_x000D__x000D_
const Routing = () => (_x000D__x000D_
<Router>_x000D__x000D_
<div>_x000D__x000D_
<Route exact path =""/"" component = {App} />_x000D__x000D_
<Route path =""/about"" component = {About} />_x000D__x000D_
</div>_x000D__x000D_
</Router>_x000D__x000D_
)_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
It works great, but when I click a link to go to /about and then hit the back button nothing happens. How do I make it so my internet browser's back button will take me back to the previous page? The app is built using create-react-app if that makes a difference.
_x000D__x000D_
",,Algorithmic
47742280,1,49603521,3135974,7,0,1,0,2017-12-10T18:50:59.987,25,23212,How to define entry point for react native app,"I'm just getting started with react native and have created a base app with create-react-native-app
.
_x000D__x000D_
_x000D__x000D_
I did some restructuring and made a few new folders and renamed the App.js to Home.js. I modified the app.json
to contain an entry point that references the new Home.js
file. When I load the app, nothing happens. There's no error, it just stays on the expo screen.
_x000D__x000D_
_x000D__x000D_
._x000D__x000D_
-components_x000D__x000D_
-screens_x000D__x000D_
-Home_x000D__x000D_
Home.js_x000D__x000D_
-config_x000D__x000D_
-node_modules_x000D__x000D_
-tests_x000D__x000D_
app.json_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
app.json file:
_x000D__x000D_
_x000D__x000D_
{_x000D__x000D_
""expo"": {_x000D__x000D_
""sdkVersion"" : ""23.0.0"",_x000D__x000D_
""entryPoint"" : ""./screens/Home/Home.js""_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
How do you define the entry point of the app?
_x000D__x000D_
",,Algorithmic
47880578,1,0,6598897,0,0,0,0,2017-12-19T05:35:12.110,1,264,Can't call native iOS method from React Native,"I want to subscribe to push notifications for the Pusher service for a particular channel, but because the react native Pusher client doesn't support push notifications, I need to write a bridge.
_x000D__x000D_
_x000D__x000D_
To this end, there's a native iOS method called 'subscribe' I want to call on an instance(?) of 'pusher' from react native, but for some reason it won't execute properly.
_x000D__x000D_
_x000D__x000D_
'pusher' is intialised in AppDelegate.m as:
_x000D__x000D_
_x000D__x000D_
self.pusher = [PTPusher pusherWithKey:@""..."" delegate:self encrypted:YES cluster:@""ap1""];_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
The exported native iOS function in AppDelegate.m I've written looks like:
_x000D__x000D_
_x000D__x000D_
RCT_EXPORT_METHOD(subscribe:(NSString *)channel) {_x000D__x000D_
[[[self pusher] nativePusher] subscribe:channel];_x000D__x000D_
NSLog(@""Called AppDelegate.subscribe with channel: %@"", channel);_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
which I call in react native using NativeModules.AppDelegate.subscribe([channel]). I can see this is being called because the log shows up, but I can tell that the nativePusher.subscribe function isn't being called because notifications don't get received, and the logging I've added within nativePusher.subscribe doesn't show up.
_x000D__x000D_
_x000D__x000D_
The strange thing is that the nativePusher.subscribe method works if it's called under a different function:
_x000D__x000D_
_x000D__x000D_
- (void)application:(UIApplication *)application _x000D__x000D_
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {_x000D__x000D_
[[[self pusher] nativePusher] registerWithDeviceToken:deviceToken];_x000D__x000D_
NSLog(@""Registered device token: %@"", deviceToken);_x000D__x000D_
[[[self pusher] nativePusher] subscribe:@""channelc""];_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
So I suspect it's something to do with the
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
application:(UIApplication *)application
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
not being used, and/or the reference to self.
_x000D__x000D_
_x000D__x000D_
Do I need to call nativePusher.subscribe differently because I'm exporting it to react native or is it something else?
_x000D__x000D_
",,Algorithmic
48007502,1,0,3531097,1,2,0,0,2017-12-28T12:25:37.093,9,13837,uri image is not rendering in React native Image view,"I am new to React native development and developing an android application using React Native. I have a profile page where I am trying to show name and contact detail like below.
_x000D__x000D_
_x000D__x000D_
return(_x000D__x000D_
<View style={{flex: 1}}>_x000D__x000D_
<ScrollView contentContainerStyle={styles.contentContainer}>_x000D__x000D_
<View style={styles.card}>_x000D__x000D_
<View style={styles.horizontalCard}>_x000D__x000D_
<Image_x000D__x000D_
style={{width: 34, height: 34}}_x000D__x000D_
source={{uri: _x000D__x000D_
'http://test.abc.com/appdummyurl/images/mobile.png'}}_x000D__x000D_
/>_x000D__x000D_
<Text style={styles.header}>{this.state.user_email}</Text>_x000D__x000D_
</View>_x000D__x000D_
_x000D__x000D_
<View style={styles.horizontalCard}>_x000D__x000D_
<Image_x000D__x000D_
style={{width: 34, height: 34}}_x000D__x000D_
source={{uri: _x000D__x000D_
'http://test.abc.com/appdummyurl/images/images/profile.png'}}_x000D__x000D_
/>_x000D__x000D_
<Text style={styles.header}>{this.state.user_no}</Text>_x000D__x000D_
</View>_x000D__x000D_
_x000D__x000D_
</View> _x000D__x000D_
_x000D__x000D_
</ScrollView>_x000D__x000D_
<TouchableOpacity onPress={this.logOut}>_x000D__x000D_
<View style={styles.BottonCard}>_x000D__x000D_
<Text style={styles.Btntext}>LOG OUT</Text>_x000D__x000D_
</View>_x000D__x000D_
</TouchableOpacity>_x000D__x000D_
</View>_x000D__x000D_
);_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Everything is working fine except the first image is loading to first Image view and for the second Image its showing the blank space. Please help me to figure it out.
_x000D__x000D_
_x000D__x000D_
Also please guide me how can I use a variable in the place of image url.
_x000D__x000D_
",,Other
48133121,1,0,8779767,1,0,1,0,2018-01-07T00:00:17.527,2,2496,Testing a component's state after an async fetch with Jest & Enzyme (using module),"I'm trying to test a React component's state with Jest._x000D__x000D_
On ComponentDidUpdate, it fetches the username on the server if someone is logged in and updates the state.user with the username._x000D__x000D_
If no user is logged in, it switches state.pleaseLogin to true.
_x000D__x000D_
_x000D__x000D_
import React from ""react"";_x000D__x000D_
import ""./Header.css"";_x000D__x000D_
import ""whatwg-fetch"";_x000D__x000D_
import LoggedInNav from ""./LoggedInNav"";_x000D__x000D_
import LoggedOutNav from ""./LoggedOutNav"";_x000D__x000D_
import urls from ""../../urls"";_x000D__x000D_
import getUser from ""../fetch/getUser"";_x000D__x000D_
_x000D__x000D_
export default class Header extends React.Component {_x000D__x000D_
constructor(props) {_x000D__x000D_
super(props);_x000D__x000D_
this.state = {_x000D__x000D_
user: null,_x000D__x000D_
pleaseLogin: false_x000D__x000D_
};_x000D__x000D_
this.handleGetUser = this.handleGetUser.bind(this);_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
handleGetUser() {_x000D__x000D_
getUser().then(res => {_x000D__x000D_
if (res.hasOwnProperty(""username"")) {_x000D__x000D_
this.setState({_x000D__x000D_
user: res.username_x000D__x000D_
});_x000D__x000D_
} else {_x000D__x000D_
this.setState({_x000D__x000D_
pleaseLogin: true_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
componentDidMount() {_x000D__x000D_
this.handleGetUser();_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
render() {_x000D__x000D_
let nav;_x000D__x000D_
if (this.state.user) {_x000D__x000D_
nav = <LoggedInNav user={this.state.user} />;_x000D__x000D_
} else if (this.state.pleaseLogin) {_x000D__x000D_
nav = <LoggedOutNav />;_x000D__x000D_
} else {_x000D__x000D_
nav = null;_x000D__x000D_
}_x000D__x000D_
return (_x000D__x000D_
<header className=""app-header"">_x000D__x000D_
<h1 className=""header-brand"">Watchers</h1>_x000D__x000D_
{nav}_x000D__x000D_
</header>_x000D__x000D_
);_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
And here is the code for the getUser() that fetches username on the server:
_x000D__x000D_
_x000D__x000D_
export default () => {_x000D__x000D_
return fetch(`${BASE_URL}/api/user/user_data`, {_x000D__x000D_
credentials: ""include""_x000D__x000D_
})_x000D__x000D_
.then(res => res.json())_x000D__x000D_
.catch(error => {_x000D__x000D_
console.log(error);_x000D__x000D_
return error;_x000D__x000D_
});_x000D__x000D_
};_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
I mocked the API call manually. The file is called 'getUser.js' and is in a mocks folder, next to the original 'getUser.js' like so:
_x000D__x000D_
_x000D__x000D_
export default () => {_x000D__x000D_
return Promise.resolve({_x000D__x000D_
username: 'Username'_x000D__x000D_
})_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
And then the test:
_x000D__x000D_
_x000D__x000D_
import React, { Component } from ""react"";_x000D__x000D_
import Enzyme, { shallow, mount } from ""enzyme"";_x000D__x000D_
import Adapter from ""enzyme-adapter-react-16"";_x000D__x000D_
import Header from ""../react/components/Header"";_x000D__x000D_
import getUser from ""../react/fetch/getUser"";_x000D__x000D_
_x000D__x000D_
Enzyme.configure({ adapter: new Adapter() });_x000D__x000D_
_x000D__x000D_
jest.mock(""../react/fetch/getUser"");_x000D__x000D_
const wrapper = mount(<Header />);_x000D__x000D_
_x000D__x000D_
describe(""<Header/>"", () => {_x000D__x000D_
describe(""component class"", () => {_x000D__x000D_
it(""should update state.user after componentDidMount()"", () => {_x000D__x000D_
return getUser().then(() => {_x000D__x000D_
expect(wrapper.state()).toEqual({_x000D__x000D_
user: ""Username"",_x000D__x000D_
pleaseLogin: false_x000D__x000D_
});_x000D__x000D_
});_x000D__x000D_
});_x000D__x000D_
});_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
The test passes, the state changed like intended with state.user now equal to ""Username""._x000D__x000D_
If I change my mock module and make it return an empty object, it works as well and state.pleaseLogin switches to true. However I have to do it manually to test both cases._x000D__x000D_
I tried to mock those two different implementations directly in my test file with jest.mock('...module', () => {}), and with jest.doMock('...module', () => {}) but the state does not update when I run the test; while it did with the manual mock. I am not sure where the issue comes from, maybe this has something to do with asynchronicity or with mocking in Jest?
_x000D__x000D_
",,Algorithmic
48259073,1,48259303,3895259,1,0,0,0,2018-01-15T08:13:08.573,0,386,Get height of the div after the state updates in React JS,"I want to get height of the div after the state is updated.
_x000D__x000D_
_x000D__x000D_
I have code as follows:
_x000D__x000D_
_x000D__x000D_
const FEATURES = ['feature1', 'feature2', 'feature3', 'feature4', 'feature5', 'feature6']_x000D__x000D_
const CHECKS = ['check1', 'check2', 'check3', 'check4', 'check5', 'check6', 'check7', 'check8', 'check9', 'check10']_x000D__x000D_
_x000D__x000D_
class Test extends React.Component {_x000D__x000D_
constructor(props){_x000D__x000D_
super(props);_x000D__x000D_
_x000D__x000D_
this.state = {_x000D__x000D_
checksView: false,_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
componentDidMount(){_x000D__x000D_
let height = document.getElementById(""view"").clientHeight;_x000D__x000D_
console.log(height);_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
changeView(view, event){_x000D__x000D_
event.preventDefault();_x000D__x000D_
if(view === ""specs""){_x000D__x000D_
this.setState({checksView: false});_x000D__x000D_
}else{_x000D__x000D_
this.setState({checksView: true});_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
renderContent(){_x000D__x000D_
if(this.state.checksView){_x000D__x000D_
return (_x000D__x000D_
<div id=""view"">_x000D__x000D_
{CHECKS.map(check => <div>{check}</div>)}_x000D__x000D_
</div>_x000D__x000D_
)_x000D__x000D_
}else{_x000D__x000D_
return (_x000D__x000D_
<div id=""view"">_x000D__x000D_
{FEATURES.map(check => <div>{check}</div>)}_x000D__x000D_
</div>_x000D__x000D_
)_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
render(){_x000D__x000D_
return (_x000D__x000D_
<div>_x000D__x000D_
<div>_x000D__x000D_
<div style={{float: ""left"", marginRight: 10}} onClick={this.changeView.bind(this, ""specs"")}>Specs</div>_x000D__x000D_
<div style={{float: ""left""}} onClick={this.changeView.bind(this, ""checks"")}>Checks</div><br />_x000D__x000D_
</div>_x000D__x000D_
{this.renderContent()} _x000D__x000D_
</div>_x000D__x000D_
)_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
React.render(<Test />, document.getElementById('container'));_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
In the console.log
in componentDidMount
I see the same value._x000D__x000D_
But the div with checks
has bigger height than the div with features, but the content of the checks isn't loaded and I still get the height of the features div.
_x000D__x000D_
_x000D__x000D_
Any idea how can I wait to content to load after the state update and then check its height?
_x000D__x000D_
_x000D__x000D_
Here is the fiddle.
_x000D__x000D_
",,Algorithmic
48385112,1,48389952,8551819,1,0,0,0,2018-01-22T15:34:30.637,1,1288,How to updateProfile for phoneNumber for an email/password account in firebase with react,"I am getting to the success block as everything goes well, but then when I check the user it seems as if the user never got updated. I have this working for displayName, but for some reason phoneNumber is not working.
_x000D__x000D_
_x000D__x000D_
Here is the code:
_x000D__x000D_
_x000D__x000D_
const user = firebase.auth().currentUser;_x000D__x000D_
_x000D__x000D_
user_x000D__x000D_
.updateProfile({ phoneNumber: '01112223333' })_x000D__x000D_
.then(() => {_x000D__x000D_
alert('success');_x000D__x000D_
console.log(user);_x000D__x000D_
})_x000D__x000D_
.catch(err => {_x000D__x000D_
alert(err);_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
",,Algorithmic
48514306,1,48514546,7239552,1,3,0,0,2018-01-30T05:14:37.790,0,4258,Can't load image from an API using React,"I successfully load data from the OpenDota API but somehow, the images _x000D__x000D_
are broken when I pass the image props in my Heroes.js
_x000D__x000D_
_x000D__x000D_
here is the Component where I load the API.
_x000D__x000D_
_x000D__x000D_
HeroStats.js
_x000D__x000D_
_x000D__x000D_
import React, { Component } from 'react'_x000D__x000D_
import Sidebar from ""./Sidebar"";_x000D__x000D_
import Heroes from ""./Heroes""_x000D__x000D_
import ""./App.css""_x000D__x000D_
import axios from ""axios"";_x000D__x000D_
_x000D__x000D_
const URL = ""https://api.opendota.com/api/heroStats"";_x000D__x000D_
_x000D__x000D_
class HeroStats extends Component {_x000D__x000D_
state = {_x000D__x000D_
data: []_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
componentDidMount() {_x000D__x000D_
axios.get(URL)_x000D__x000D_
.then(res => {_x000D__x000D_
this.setState({_x000D__x000D_
data: res.data_x000D__x000D_
});_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
render() {_x000D__x000D_
const Stats = this.state.data.map(stat => (_x000D__x000D_
<Heroes_x000D__x000D_
key={stat.id}_x000D__x000D_
id={stat.id}_x000D__x000D_
name={stat.name}_x000D__x000D_
localized_name={stat.localized_name}_x000D__x000D_
img={stat.img}_x000D__x000D_
icon={stat.icon}_x000D__x000D_
pro_win={stat.pro_win}_x000D__x000D_
pro_pick={stat.pro_pick}_x000D__x000D_
pro_ban={stat.pro_ban}_x000D__x000D_
_x000D__x000D_
/>_x000D__x000D_
))_x000D__x000D_
return (_x000D__x000D_
<div>_x000D__x000D_
{Stats}_x000D__x000D_
</div>_x000D__x000D_
)_x000D__x000D_
}_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
export default HeroStats;_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
and here where I pass my props._x000D__x000D_
Heroes.js
_x000D__x000D_
_x000D__x000D_
import React from 'react'_x000D__x000D_
_x000D__x000D_
const Heroes = (props) => (_x000D__x000D_
<div>_x000D__x000D_
_x000D__x000D_
<h1>{props.localized_name}</h1>_x000D__x000D_
<img src={props.img} />_x000D__x000D_
<img src={props.icon} />_x000D__x000D_
<h1>{props.pro_win}</h1>_x000D__x000D_
<h1>{props.pro_pick}</h1>_x000D__x000D_
<h1>{props.pro_ban}</h1>_x000D__x000D_
</div>_x000D__x000D_
_x000D__x000D_
)_x000D__x000D_
_x000D__x000D_
export default Heroes;_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
also if I use other tag like <h1>{props.img}</h1>
it shows the image file path. did I miss something that i should include?
_x000D__x000D_
",,Misconception
48642733,1,0,2439580,1,8,2,0,2018-02-06T12:07:10.067,11,4079,"Jest with jsdom, document is undefined inside Promise resolve","The scenario
_x000D__x000D_
_x000D__x000D_
Trying to test a simple React component using Jest (and Enzyme). This component uses react-dropzone
and I want to test some operations involving the DOM so I use jsdom (as already configured by create-react-app
)
_x000D__x000D_
_x000D__x000D_
The problem
_x000D__x000D_
_x000D__x000D_
The document
object while available in the my test code and also available inside of the component, is undefined
inside of the dropzone onDrop
callback, which prevents the test from running.
_x000D__x000D_
_x000D__x000D_
The code
_x000D__x000D_
_x000D__x000D_
MyDropzone
_x000D__x000D_
_x000D__x000D_
import React from 'react'_x000D__x000D_
import Dropzone from 'react-dropzone'_x000D__x000D_
_x000D__x000D_
const MyDropzone = () => {_x000D__x000D_
const onDrop = ( files ) =>{_x000D__x000D_
fileToBase64({file: files[0]})_x000D__x000D_
.then(base64Url => {_x000D__x000D_
return resizeBase64Img({base64Url})_x000D__x000D_
})_x000D__x000D_
.then( resizedURL => {_x000D__x000D_
console.log(resizedURL.substr(0, 50))_x000D__x000D_
})_x000D__x000D_
}_x000D__x000D_
return (_x000D__x000D_
<div>_x000D__x000D_
<Dropzone onDrop={onDrop}>_x000D__x000D_
Some text_x000D__x000D_
</Dropzone>_x000D__x000D_
</div>_x000D__x000D_
);_x000D__x000D_
};_x000D__x000D_
_x000D__x000D_
const fileToBase64 = ({file}) => {_x000D__x000D_
return new Promise((resolve, reject) => {_x000D__x000D_
const reader = new FileReader()_x000D__x000D_
reader.onload = () => {_x000D__x000D_
return resolve(reader.result)_x000D__x000D_
}_x000D__x000D_
reader.onerror = (error) => {_x000D__x000D_
return reject(error)_x000D__x000D_
}_x000D__x000D_
reader.readAsDataURL(file)_x000D__x000D_
})_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
/**_x000D__x000D_
* Resize base64 image to width and height,_x000D__x000D_
* keeping the original image proportions_x000D__x000D_
* with the width winning over the height_x000D__x000D_
*_x000D__x000D_
*/_x000D__x000D_
const resizeBase64Img = ({base64Url, width = 50}) => {_x000D__x000D_
const canvas = document.createElement('canvas')_x000D__x000D_
canvas.width = width_x000D__x000D_
const context = canvas.getContext('2d')_x000D__x000D_
const img = new Image()_x000D__x000D_
_x000D__x000D_
return new Promise((resolve, reject) => {_x000D__x000D_
img.onload = () => {_x000D__x000D_
const imgH = img.height_x000D__x000D_
const imgW = img.width_x000D__x000D_
const ratio = imgW / imgH_x000D__x000D_
canvas.height = width / ratio_x000D__x000D_
context.scale(canvas.width / imgW, canvas.height / imgH)_x000D__x000D_
context.drawImage(img, 0, 0)_x000D__x000D_
resolve(canvas.toDataURL())_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
img.onerror = (error) => {_x000D__x000D_
reject(error)_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
img.src = base64Url_x000D__x000D_
})_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
export default MyDropzone;_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
MyDropzone.test.jsx
_x000D__x000D_
_x000D__x000D_
import React from 'react'_x000D__x000D_
import { mount } from 'enzyme'_x000D__x000D_
import Dropzone from 'react-dropzone'_x000D__x000D_
_x000D__x000D_
import MyDropzone from '../MyDropzone'_x000D__x000D_
_x000D__x000D_
describe('DropzoneInput component', () => {_x000D__x000D_
it('Mounts', () => {_x000D__x000D_
const comp = mount(<MyDropzone />)_x000D__x000D_
const dz = comp.find(Dropzone)_x000D__x000D_
const file = new File([''], 'testfile.jpg')_x000D__x000D_
console.log(document)_x000D__x000D_
dz.props().onDrop([file])_x000D__x000D_
})_x000D__x000D_
})_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
setupJest.js
_x000D__x000D_
_x000D__x000D_
import { configure } from 'enzyme'_x000D__x000D_
import Adapter from 'enzyme-adapter-react-16'_x000D__x000D_
_x000D__x000D_
configure({ adapter: new Adapter() })_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Config
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
- Default
create-react-app
jest config with setupJest.js
added to setupFiles
_x000D__x000D_
- Run: yarn test
_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
Error
_x000D__x000D_
_x000D__x000D_
TypeError: Cannot read property 'createElement' of undefined_x000D__x000D_
at resizeBase64Img (C:\dev\html\sandbox\src\MyDropzone.jsx:44:29)_x000D__x000D_
at fileToBase64.then.base64Url (C:\dev\html\sandbox\src\MyDropzone.jsx:8:20)_x000D__x000D_
at <anonymous>_x000D__x000D_
at process._tickCallback (internal/process/next_tick.js:188:7)_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
More info
_x000D__x000D_
_x000D__x000D_
Consider that document
is always defined if running that code in the browser, so to me the issue seems related with jsdom or Jest.
_x000D__x000D_
_x000D__x000D_
I am not sure if it is related with Promises, with the FileReaded or with the JS scope in general.
_x000D__x000D_
_x000D__x000D_
Maybe a bug on Jest side ?
_x000D__x000D_
",,Misconception
48777326,1,0,4241640,1,6,0,0,2018-02-13T23:25:41.983,1,1075,"React Native Image Picker cannot read property ""showImagePicker"" of undefined","I've followed all of the iOS installation instructions for the package react-native-image-picker
, but it still errors any time I try to use the showImagePicker()
method.
_x000D__x000D_
_x000D__x000D_
It tells me it cannot read property ""showImagePicker"" of undefined
.
_x000D__x000D_
_x000D__x000D_
I have the package imported at the top of my file var ImagePicker = require('react-native-image-picker');
, I have done react-native link
multiple times, I have added RNImagePicker.xcodeproj
to my libraries folder, I have added RNImagePicker.a
to ""Link Binary with Libraries"", I have added the permissions to my info.plist
file.
_x000D__x000D_
_x000D__x000D_
I have done everything in the documentation, but still nothing works.
_x000D__x000D_
_x000D__x000D_
Here is my JS:
_x000D__x000D_
_x000D__x000D_
import React from 'react';_x000D__x000D_
import PropTypes from 'prop-types';_x000D__x000D_
import { StyleSheet, Text, View, Image, ScrollView, TextInput, TouchableOpacity, KeyboardAvoidingView } from 'react-native';_x000D__x000D_
_x000D__x000D_
var ImagePicker = require('react-native-image-picker');_x000D__x000D_
_x000D__x000D_
export default class ProfilePicUploaderPage extends React.Component {_x000D__x000D_
state = {_x000D__x000D_
ProfilePicLocalSource: null,_x000D__x000D_
ProfilePicRemoteSource: null_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
ChoosePhoto(){_x000D__x000D_
console.log(SOCButton);_x000D__x000D_
var options = {_x000D__x000D_
title: 'Select Avatar',_x000D__x000D_
customButtons: [_x000D__x000D_
{name: 'fb', title: 'Choose Photo from Facebook'},_x000D__x000D_
],_x000D__x000D_
storageOptions: {_x000D__x000D_
skipBackup: true,_x000D__x000D_
path: 'images'_x000D__x000D_
}_x000D__x000D_
};_x000D__x000D_
ImagePicker.showImagePicker(options, (response) => {_x000D__x000D_
console.log('Response = ', response);_x000D__x000D_
_x000D__x000D_
if (response.didCancel) {_x000D__x000D_
console.log('User cancelled image picker');_x000D__x000D_
}_x000D__x000D_
else if (response.error) {_x000D__x000D_
console.log('ImagePicker Error: ', response.error);_x000D__x000D_
}_x000D__x000D_
else if (response.customButton) {_x000D__x000D_
console.log('User tapped custom button: ', response.customButton);_x000D__x000D_
}_x000D__x000D_
else {_x000D__x000D_
let source = { uri: response.uri };_x000D__x000D_
_x000D__x000D_
this.setState({_x000D__x000D_
avatarSource: source_x000D__x000D_
});_x000D__x000D_
}_x000D__x000D_
});_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
render(){_x000D__x000D_
return(<all of my JSX>);_x000D__x000D_
}_x000D__x000D_
_x000D__x000D_
_x000D__x000D_
The only lead I have as to what's happening is that when I try to build my app in Xcode, it fails with the error clang: error: linker command failed with exit code 1 (use -v to see invocation)
.
_x000D__x000D_
_x000D__x000D_
Does anyone have an idea of what's going on?
_x000D__x000D_
",