React: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
|||
Line 37: | Line 37: | ||
} | } | ||
}; | }; | ||
</source> | |||
=Passing Props to child= | |||
Parent.json | |||
<source> | |||
import React from 'react'; | |||
import ReactDOM from 'react-dom'; | |||
import { Child } from './Child'; | |||
class Parent extends React.Component { | |||
constructor(props) { | |||
super(props); | |||
this.state = { name: 'Frarthur' }; | |||
} | |||
render() { | |||
return <Child name={this.state.name} />; | |||
} | |||
} | |||
ReactDOM.render(<Parent />, document.getElementById('app')); | |||
</source> | |||
Child.json | |||
<source> | |||
import React from 'react'; | |||
export class Child extends React.Component { | |||
render() { | |||
return <h1>Hey, my name is {this.props.name}!</h1>; | |||
} | |||
} | |||
</source> | </source> |
Revision as of 06:16, 23 October 2019
Quick Reference
Variables
const myVar = (
<h1>Love it</h1>
);
Run
Maps
// Only do this if items have no stable IDs
const todo = ["fix", "syntax", "highlighting"];
const todoItems = todos.map((todo, index) =>
<li key={index}>
{todo.text}
</li>
);
Component Example
import React, {Component} from 'react';
import ReactDOM from 'react-dom';
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = {name: "Frarthur"};
}
render() {
return <div></div>;
}
};
Passing Props to child
Parent.json
import React from 'react';
import ReactDOM from 'react-dom';
import { Child } from './Child';
class Parent extends React.Component {
constructor(props) {
super(props);
this.state = { name: 'Frarthur' };
}
render() {
return <Child name={this.state.name} />;
}
}
ReactDOM.render(<Parent />, document.getElementById('app'));
Child.json
import React from 'react';
export class Child extends React.Component {
render() {
return <h1>Hey, my name is {this.props.name}!</h1>;
}
}