Rendering Order In React - reactjs

I am trying to understand the rendering order of Methods in React. I have a "persons" class and I iterate an array inside it calling a child component called "person".
My Persons.js in below
import React, { Component } from 'react';
import Person from './Person/Person';
class Persons extends Component{
constructor(props){
super(props);
console.log('[Persons.js] inside constructor',props);
}
componentWillMount(){
console.log('[Persons.js] inside componentWillMount()');
}
componentDidMount(){
console.log('[Persons.js] inside componentDidMount()');
}
render(){
console.log('[Persons.js] inside render())');
return(
this.props.persons.map((person,index)=>{
return(
<Person click={() => this.props.clicked(index)} name={person.name} age={person.age} change={ (event) => this.props.changed(event,index)} key={person.id}/>
);
})
);
}
}
export default Persons;
My Person.js is below
import React, { Component } from 'react';
import classes from './Person.css'
class Person extends Component{
constructor(props){
super(props);
console.log('[Person.js] inside constructor',props);
}
componentWillMount(){
console.log('[Person.js] inside componentWillMount()');
}
componentDidMount(){
console.log('[Person.js] inside componentDidMount()');
}
render(){
console.log('[Person.js] inside render()');
return(
<div className={classes.Person}>
<p onClick={this.props.click}>I'm {this.props.name} and {this.props.age} years old.</p>
<input type="text" onChange={this.props.change} value={this.props.name}/>
</div>
);
}
}
export default Person;
All I want to know is, why doesn't it invoke componentDidMount after render method of Person.js file? Instead, it invokes 4 times after finish iterating.
Here is the screenshot
Any idea?

Related

How can I pass data from a component array to an another component with props?

I need a little help. I'm working on a project that uses Class Components in React and I got stuck with a issue.
How can I pass datas using props?
For example, imagine that I have one Component that have an array in the state:
import React,{Component} from "react";
class CarList extends Component{
constructor(props){
super(props);
this.state = {
carList: ['Jeep', 'Kwid','HB20','Ônix', 'Prisma', 'Gol quadrado']
}
}
render(){
return(
<div>
</div>
);
}
}
export default CarList;
And now I have to call this array in a Option Tag inside a Select Tag.
Let's imagine this component Bellow:
import React from "react";
import { Component } from "react";
import CarList from "./components/Datas";
class App extends Component{
render(){
return(
<div>
<p>I got it! Here is the car list:</p>
<select>
{this.state.CarList.map( (item,x)=>{
return(
<option key={x}>{item}</option>
)
})}
</select>
</div>
)
}
}
export default App;
This piece of code does not work.
the console.log says: "Uncaught TypeError: this.state is null"
I know that I could create a div with my datas and call with , but I have to use props to pass the datas between the Components.
How can I create a callback function using props to resolve this?
Hi!
I tried to call using this.state, but I got "this.state is not defined"
To pass your data as a props you have to pass it to your child component like this from your parent component :
class ParentComponent extends React.Component {
state = {
carList: [],
};
constructor(props) {
super(props);
this.state = {
carList: ['Jeep', 'Kwid', 'HB20', 'Ônix', 'Prisma', 'Gol quadrado'],
};
}
render() {;
return (
<div>
<ChildComponent carList={this.state.carList || []} />
</div>
);
}
}
and then it is accessible in your child component with this.props.
you can use this props in child component like this:
class ChildComponent extends React.Component {
render() {
return (
<div>
{this.props.carList.map((cars, index) => {
return (
<span key={index}>
{cars}
<br />
</span>
);
})}
</div>
);
}
}
Edit -
if you want to see source code : https://stackblitz.com/edit/react-ts-ddcylu?file=Parent.tsx

How can I call an array from one component to show in another component using Class Component in React-js?

I have a question that I believe that is simple, buuut I couldn't solve it and I didn't find something similar on the internet.
I have a React Component that have an array like state and I'm trying to take this array and put in another array into a select-option.
This is the Component with the array:
import React,{Component} from "react";
class CarList extends Component{
constructor(props){
super(props);
this.state = {
carList: ['Jeep', 'Kwid','HB20','Ônix', 'Prisma', 'Gol quadrado']
}
}
render(){
return(
<div>
<select>
{this.state.carList.map( (item,ii)=>{
return(
<option key={ii}>{item}</option>
)
} )}
</select>
</div>
);
}
}
export default CarList;
And this is the Component that is render on React-dom:
import React from "react";
import { Component } from "react";
import CarList from "./components/Tabela";
class App extends Component{
constructor(props){
super(props);
this.state={
}
}
render(){
return(
<div>
<p>I got it! Here is the car list:</p>
<select>
{this.state.carList.map( (item,x)=>{
return(
<option key={x}>{item}</option>
)
})}
</select>
</div>
)
}
}
export default App;```
Hey!
I tried to call with a map();
I tried to import the component;
I tried to call the array just before import the component;
Not sure if I understand you correctly. Here is an option:
From checking your App component code, the select component would give the same result as this:
render() {
return (
<div>
<p>I got it! Here is the car list:</p>
<CarList />
</div>
);
}

React static getDrivedStateFromProps is not working in React 16.13.1

Hi i am new to reactjs my react version=16.13.1.There are three files "Student.js",:Marks.js","index.js". I am trying to update state in Marks.js through Students.js using button. I am trying to update marks state through method "static getDrivedStateFromProps()".But this method is not working not even the console.log inside this method is working.Any help or suggestion will be appriciated.
**Index.js**
import React from 'react';
import ReactDOM from 'react-dom';
import Student from "./Student";
ReactDOM.render(<Student roll_no="101"/>,document.getElementById("root"));
**Student.js**
import React, { Component } from 'react'
import Marks from "./Marks";
export default class Student extends Component {
constructor(props){
super(props);
this.state={
roll_no:this.props.roll_no
}
}
handleClick=()=>{
console.log("button click");
this.setState({roll_no:(Number(this.state.roll_no)+2)});
}
render() {
return (
<div>
<h1>This rollNo is {this.state.roll_no}</h1>
<Marks roll_no={this.state.roll_no}/>
<button onClick={this.handleClick}>Click Me</button>
</div>
)
}
}
**Marks.js**
import React, { Component } from 'react';
export default class Marks extends Component {
constructor(props){
super(props);
console.log("this is marks constructor"+this.props.roll_no);
this.state={
mroll_no:this.props.roll_no
};
}
static getDrivedStateFromProps(props,state){
console.log("this is in marks in getDrivedStateFromProps");
console.log(props.roll_no);
console.log(state.mroll_no);
if(props.roll_no!==state.mroll_no){
return {mroll_no:props.roll_no}
}
return null;
}
render() {
console.log("inside marks render");
console.log("inside marks render this.props.roll_no"+this.props.roll_no);
return (
<div>
<p> in Marks the roll_no is{this.state.mroll_no}</p>
</div>
)
}
}

Props missing after passing to children when using draft-js

I'm kinda new to react and thought that in the constructor function, using super(props) can fully receive the props that passed by parents. But however, I can't get the string test from my parent component.
So in the parent component, I pass the string "test" as props
import React from 'react';
import Post from '../components/post';
import "../components/css/post.css"
class Bulletin extends React.Component {
render()
{
console.log(this.props);
return (
<div>
<Post test={"sent from parent"}/>
</div>
);
}
}
export default Bulletin;
And then in Post.js, I print the props in two places:
import React, { Component } from 'react';
export default class Edit extends Component {
constructor(props) {
super(props);
console.log(props);
}
render() {
console.log(this.props);
return (
<div className="editor" onClick={this.focus}>
</div>
);
}
}
The two outputs are both {className: "editor"} which is not what I need. I need the string {test: "sent from parent"} and don't know why this doesn't works for me.

How to send data from one component to another component in react without parent child relation

Home Component
import React, { Component } from 'react';
import Two from 'components/Two.jsx'
import One from 'components/One.jsx'
class Home extends Component {
constructor(props){
super(props)
this.state = {
}
this.one = React.createRef();
}
getData = (data) => {
this.one.current.finalClick(data);
}
render() {
return (
<div>
<Two shareData={this.getData} />
<One ref={this.one} />
</div>
);
}
}
export default Home;
One Compoent:
import React from 'react'
class One extends React.Component {
constructor(props){
super(props);
this.state={
}
}
finalClick = (data) => {
console.log(data)
}
render(){
return (
<div>
</div>
)
}
}
export default One;
Two Component:
import React from 'react'
class Two extends React.Component {
handleClick = (data) => {
this.props.shareData(data)
}
render(){
return (
<div>
<br/>
<button className="btn btn-info" onClick={() => this.handleClick('hellow')}>click</button>
</div>
)
}
}
export default Two;
Here i am tring to get click one function from from another components.
which is not it's parent or child componet.
First I am clicking the button in two and it is sending data to Home Component.
And it is logging the data.
And i wants to send data to finalClick() method in the same time after getData() called in Home Component.
My finalClick() function in One Component getting clicked but data is not going.
Save the data that you want to share in the state of Home. Pass a function from Home to Two through Two's props in order to update the data. Pass the data from Home's state down to One in the props.

Resources