ReactNative: how to center text?

From headline‘ style remove height, justifyContent and alignItems. It will center the text vertically. Add textAlign: ‘center’ and it will center the text horizontally. headline: { textAlign: ‘center’, // <– the magic fontWeight: ‘bold’, fontSize: 18, marginTop: 0, width: 200, backgroundColor: ‘yellow’, }

Having services in React application

The issue becomes extremely simple when you realize that an Angular service is just an object which delivers a set of context-independent methods. It’s just the Angular DI mechanism which makes it look more complicated. The DI is useful as it takes care of creating and maintaining instances for you but you don’t really need … Read more

Get form data in ReactJS

There are a few ways to do this: 1) Get values from array of form elements by index handleSubmit = (event) => { event.preventDefault(); console.log(event.target[0].value) } 2) Using name attribute in html handleSubmit = (event) => { event.preventDefault(); console.log(event.target.elements.username.value) // from elements property console.log(event.target.username.value) // or directly } <input type=”text” name=”username”/> 3) Using refs handleSubmit … Read more

“SyntaxError: Unexpected token < in JSON at position 0"

The wording of the error message corresponds to what you get from Google Chrome when you run JSON.parse(‘<…’). I know you said the server is setting Content-Type:application/json, but I am led to believe the response body is actually HTML. Feed.js:94 undefined “parsererror” “SyntaxError: Unexpected token < in JSON at position 0” with the line console.error(this.props.url, … Read more

What does the error “JSX element type ‘…’ does not have any construct or call signatures” mean?

This is a confusion between constructors and instances. Remember that when you write a component in React: class Greeter extends React.Component<any, any> { render() { return <div>Hello, {this.props.whoToGreet}</div>; } } You use it this way: return <Greeter whoToGreet=”world” />; You don’t use it this way: let Greet = new Greeter(); return <Greet whoToGreet=”world” />; In … Read more

to call onChange event after pressing Enter key

According to React Doc, you could listen to keyboard events, like onKeyPress or onKeyUp, not onChange. var Input = React.createClass({ render: function () { return <input type=”text” onKeyDown={this._handleKeyDown} />; }, _handleKeyDown: function(e) { if (e.key === ‘Enter’) { console.log(‘do validate’); } } }); Update: Use React.Component Here is the code using React.Component which does the … Read more

Invalid Host Header when ngrok tries to connect to React dev server

I’m encountering a similar issue and found two solutions that work as far as viewing the application directly in a browser ngrok http 8080 –host-header=”localhost:8080″ ngrok http –host-header=rewrite 8080 obviously, replace 8080 with whatever port you’re running on this solution still raises an error when I use this in an embedded page, that pulls the … Read more

Get viewport/window height in ReactJS

Using Hooks (React 16.8.0+) Create a useWindowDimensions hook. import { useState, useEffect } from ‘react’; function getWindowDimensions() { const { innerWidth: width, innerHeight: height } = window; return { width, height }; } export default function useWindowDimensions() { const [windowDimensions, setWindowDimensions] = useState(getWindowDimensions()); useEffect(() => { function handleResize() { setWindowDimensions(getWindowDimensions()); } window.addEventListener(‘resize’, handleResize); return () … Read more

How to use `setState` callback on react hooks

You need to use useEffect hook to achieve this. const [counter, setCounter] = useState(0); const doSomething = () => { setCounter(123); } useEffect(() => { console.log(‘Do something after counter has changed’, counter); }, [counter]); If you want the useEffect callback to be ignored during the first initial render, then modify the code accordingly: import React, … Read more