React Components Explained – codeburst

  • Something like thisimport React from ‘react’;class MyComponent extends React.Component { render () { return div This is a component /div }}class MyOtherComponent extends React.Component { render () { return ( div MyComponent / /div ) }}This way you are able to compose more complex and useful user interface for your…
  • Component’s render method return JSX which then use to create real HTML output which will be rendered in the browser.The interesting Thing about render method is that it runs every time when your component State or Props updates.
  • Let me show you a exampleimport React from ‘react’;class MyComponent extends React.Component { constructor(props) { super(props); this.state = { name: “Manoj” }; } render () { return div My name is {this.state.name} /div }}// if we render this component the output will beMy name is ManojIgnore the super(props) (out of…
  • Let’s see an example.class MyComponent extends React.Component { constructor(props) { super(props); this.state = { name: “Manoj” }; this.changeName = this.changeName.bind(this); } changeName () { this.setState({ name: “Your Name” }); } render () { return div onClick={this.changeName} My name is {this.state.name} /div }}In the above code we are telling our React…
  • You can run this code on jsFiddle here.PropsVisualise props as options that can be passed to a component to customize its functionality.For example, I have a heading component which renders a heading with subtitle.class MyHeading extends React.Component { render () { return div h1This is a heading/h1 pSubtitle/p /div }}If…

The simplest library I ever used in my life is React. As you know React is based upon component design. Everything in React is a component which makes it easy to reuse components frequently. You can…
Continue reading “React Components Explained – codeburst”

Pop Up component – Victor.V.P. – Medium

Pop Up component  #frontenddevelopment #react #webdevelopment #reactjs

  • В примере реализовано закрытие PopUp при нажатии как на кнопку “Close” так и на document.
  • // CLASS POPUPclass PopUp extends React.Component { constructor() { super(); this.onDocumentClick = }componentDidMount() { this.onDocumentClick); }componentWillUnmount() { this.onDocumentClick); }onDocumentClick (event) { let pointer = event.target; const element = ReactDOM.findDOMNode(this); while (pointer !
  • == document pointer) { if (pointer === element) { return; } pointer = pointer.parentNode; } this.props.onClose(); //Закрываем PopUp при клике на document }render() { return div className=”popUpContainer” button className=”closeBtn” onClick={ () = { this.props.onClose this.props.onClose(); }} CLOSE/button {this.props.children} /div; }}Родительский компонент// CLASS PARENT COMPONENTclass ParentComponent extends React.Component {constructor(props) { super(props);…
  • materialsPopup}); } }Open PopUp/div;return div className=”root” div className=”header” OpenBtn/ { !!
  • materialsPopup div className=”bg” PopUp className=”materialsPopUp” onClose={ () = { this.setState({ materialsPopup: false }); } } h2This is Pop Up/h2 pYou can close the dialog by clicking on the button or the area around the dialog box/p /PopUp /div } /div /div; }}

Cоздания компонента PopUp в react. В примере реализовано закрытие PopUp при нажатии как на кнопку “Close” так и на document.
Continue reading “Pop Up component – Victor.V.P. – Medium”

6 Simple ways to speed up your react native app. – codeburst

6 Simple ways to speed up your #reactnative app.  #redux

  • shouldComponentUpdate life-cycle method is used in regular non pure React Component to cancel the re-render by returning false in certain scenarios.Following two code samples do the same thing.class MyComponent extends React.PureComponent { //}class MyComponent extends React.Component { // nextState) { if(this.props.firstProp === nextProps.
  • firstProp this.props.secondProp === nextProps.secondProp) { return false; } return true } //}Both of the above examples can help you save some wasted renders.
  • Having a unique key on every list item, saves react re-rendering it again.class MyComponent extends React.PureComponent { render() { return this.props.data.map((item, i) = { return Text key={item.id}{item.title}/Text }); }}3.
  • Bind early and don’t create functions inside render.Do thisclass MyComponent extends React.PureComponent { constructor(props) { super(props); this.doWork = this.doWork.bind(this); } doWork() { // doing some work here.
  • // this.props.dispatch…. } render() { return Text onPress={this.doWork}Do Some Work/Text } }Don’t do this inside render.Text onPress={ () = this.doWork() }Do Some Work/TextorText onPress={ this.doWork.bind(this) }Do Some Work/TextBecause render is called very often and every time you do any of the two things above, a new function is created.4.

In my previous article I have talked about hacking event loop for improving performance of your app. Your goal is to retain 60 FPS throughout. All of that applies to react or react native app as well…
Continue reading “6 Simple ways to speed up your react native app. – codeburst”

Impress Your Friends With Code Splitting in React – Hacker Noon

Impress Your Friends With #Code Splitting in #ReactJS:  by @burkeholland #JavaScript

  • Both of those actions show the edit form.First I am going to add a spot in my state for this “EditForm” component to live.class Heroes extends Component { constructor(props) { super(props); this.state = { …, lazyEditHero: null } } …, render() { return ( … ) }}I put mine in…
  • I have put this in a function called LoadEditFormclass Heroes extends Component { constructor(props) { super(props); this.state = { … lazyEditHero: null } } async LoadEditForm() { const { default : EditHero } = await import(‘.
  • /EditHero’); this.setState({ lazyEditHero: EditHero }) } render() { return ( … ) }}Now we just need to call this LoadEditForm from the two functions that trigger the editor component to be shown.class Heroes extends Component { constructor(props) { super(props); this.state = { … lazyEditHero: null } } async LoadEditForm() {…
  • /EditHero’); this.setState({ lazyEditHero: EditHero }) } handleSelect = async hero = { await this.LoadEditForm(); this.setState({ selectedHero: hero }); } handleEnableAddMode = async () = { await this.LoadEditForm(); this.setState({ addingHero: true, selectedHero: { id: ”, name: ”, saying: ” } }); } … render() { return ( … ) }}A few…
  • This also allows us to pass any props to our component when it is so lazily loaded.class Heroes extends Component { constructor(props) { super(props); this.state = { … lazyEditHero: null } } async LoadEditForm() { const { default : EditHero } = await import(‘.

In preparation for the DevUp keynote a few weeks ago, I took some time to learn the minimum amount possible to demonstrate code splitting in React. Now your first reaction is probably… As part of the…
Continue reading “Impress Your Friends With Code Splitting in React – Hacker Noon”

Common pitfall in initialising state based on props in React JS

Common pitfall in initialising state based on props in React JS  #javascript #tech #hacker

  • Common pitfall in initialising state based on props in React JSRecently I ran into this issue while working on React JS project where I was passing a part of parent component state to child component via props.
  • So the problem was when the parent component re-renders on state change, the child component is not rendered with updated state value.
  • For example —// our child componentclass ChildComponent extends Component { constructor(){ super(props); this.state = { count:this.props.value }; } render(){ return( div pI have {this.state.count} candies!
  • So the state does not update even though child component is re-rendered upon receiving new props form parent component.
  • If you need to update the state in response to prop changes (for example, to reset it), you may compare this.props and nextProps and perform state transitions using this.setState() in this methodnextProps is the latest props received from the parent component.We will use this life cycle method and update the…

Recently I ran into this issue while working on React JS project where I was passing a part of parent component state to child component via props. Then I was using that prop to set the initial state…
Continue reading “Common pitfall in initialising state based on props in React JS”

Key concept and Its necessities in React component – Shubham Akodiya – Medium

Key concept and Its necessities in React component  #es6 #react #reactjs

  • /ChildKey’;class ParentKey extends Component { constructor() { super(); this.state = {activeIndex: 0}; } updateIndex = () = { const {activeIndex} = this.state; this.setState({activeIndex: activeIndex + 1}) }getContent() { const {activeIndex} = this.state; let element; switch(activeIndex) { case 0: element = ChildKey name=’abc’ updateData={this.updateIndex} / break; case 1: element = ChildKey…
  • getContent() method returns the ChildKey component as per the activeIndex value.So Because the activeIndex default value initially set to 0, The initially getContent method returnselement = ChildKey name=’abc’ ChildKey component, we’ve initiallize the state i.e state = {childName: props.name} and when clicking on change parent component state link, then parent…
  • So name ‘bcd’ is printed and the element var becmes like below -element = ChildKey name=’bcd’ here we’re not using the key props, In ChildKey component.
  • Here react assuming both are same, So when constructor is call (constructor is called only one time in component life cycle), It initializes the state childName to ‘abc’ and In second time (when activeIndex changes to 1) constructor is not called, because react don’t know that It is separate component….
  • /ChildKey’;class ParentKey extends Component { constructor() { super(); this.state = {activeIndex: 0}; } updateIndex = () = { const {activeIndex} = this.state; this.setState({activeIndex: activeIndex + 1}) }getContent() { const {activeIndex} = this.state; let element; switch(activeIndex) { case 0: element = ChildKey name=’abc’ key=’23422%%%%’ updateData={this.updateIndex} / break; case 1: element =…

Most of the user had faced the problem of ‘key error’ i.e Warning: Each child in an array or iterator should have a unique “key” prop. Check the render method of xyz component, while using the custom…
Continue reading “Key concept and Its necessities in React component – Shubham Akodiya – Medium”

Controlled Forms in React – Byte-sized React – Medium

Controlled Forms in React  #tech #webdevelopment #programming #javascript #react #reactjs

  • Defining a form’s input value via state is considered a controlled component.For controlled inputs you will need a corresponding state and then a class method to update that state with changes.Let’s walk through a quick example with CodePen.
  • If you don’t know how to set up CodePen with React, you can check out this story.Let’s also add Boostrap 4 to our CodePen so our forms aren’t so plain.Add this to your JS file:class App extends React.Component { constructor(props) { super(props); } render() { return ( div className=”form-group container” labelControlled Form Input/label input type=”text” className=”form-control” aria-describedby=”emailHelp” placeholder=”Update input here” / large className=”form-text text-muted” Form Output /large /div ); }}ReactDOM.render( App /, your CodePen should look like this:Right now our form input is not controlled- meaning that the value of the form input is not determined by the state.
  • Let’s add a state value for the form making it a controlled form.Declare the state value in your constructor.constructor(props) { super(props); this.state({input: ”}); }Then let’s pass the state input value down to the value of the email form and as the value of our output text.input type=”text” className=”form-control” aria-describedby=”emailHelp” placeholder=”Update input here” value={this.state.input}/ large className=”form-text text-muted” {this.state.input} /largeNow notice how the value of the form reflects the state, but the form value doesn’t update.
  • Let’s add a method to update state, and pass it down using the onChange event handler.Add the method to your constructor.constructor(props) { super(props); this.state = ({input: ”}); this.formUpdate = define the formUpdate method.formUpdate(event) { this.setState({input: event.target.value});}The formUpdate function needs to be called whenever a change is made to the form input.
  • Use the onChange react event to trigger the formUpdate method.input type=”text” className=”form-control” aria-describedby=”emailHelp” placeholder=”Update input here” value={this.state.input} time a change happens to the form the formUpdate method will be called with the event object.

Form input values can be set to state values and then updated via React events. Defining a form’s input value via state is considered a controlled component. For controlled inputs you will need a…
Continue reading “Controlled Forms in React – Byte-sized React – Medium”