Open mhaidarhanif opened 5 years ago
import React, { Component } from "react";
import { Title } from "./components/Title"; // exported function is import in curly braces {} and can be more than one example {Title, SubTitle}
import StatelessCompWithClass from "./components/statelessComp"; // exported class is import without curly braces {}
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
number: 0,
error: "nomor sudah negatif"
};
// this.increaseNumber = this.increaseNumber.bind(this) //no need to write this if the function is already arrow function
}
increaseNumber = parameter => { //no need to bind this function if function is already using arrow function
this.setState({
number: this.state.number + parameter
});
};
decreaseNumber = () => {
this.setState({
number: this.state.number - 1
});
if (this.state.number < 0) {
alert(this.state.error);
}
};
render() {
console.log("ini console.lognya", this.state);
return (
<div>
<Title>Counter</Title> {/* text inside is a children of Title's element */}
<StatelessCompWithClass //element StatelessComp (as a child element)
number={this.state.number} //this is a props of StatelessComp
increaseNumber={parameter => this.increaseNumber(parameter)} //function with parameter thrown from parent element to child element as a props (StatelessComp)
decreaseNumber={this.decreaseNumber} //this is a props of StatelessComp
/>
</div>
);
}
}
import React from 'react'
export function Title(props){
return <h1>{props.children}</h1>
}
export function SubTitle(props){
return <h2>{props.children}</h2>
}
import React from "react";
export default class StatelessCompWithClass extends React.Component {
render() {
const { number, increaseNumber } = this.props; //where accessing props in class, have to use this.props
return (
<div>
<div>{number}</div>
<button onClick={() => increaseNumber(3)}>Increment</button>
<button onClick={this.props.decreaseNumber}>Decrement</button>
</div>
);
}
}
export function StatelessCompWithFunction(props){ // where accessing props in function, just use props
return (
<div>
<div>{props.number}</div>
<button onClick={() => props.increaseNumber(3)}>Increment</button>
<button onClick={props.decreaseNumber}>Decrement</button>
</div>
);
}
Mas @mhaidarh review dunk react data, react form, redux, react redux tapi pake project bnerean,, kalo counter kurang ngerti kita.... kayak yg taman safari gtu
@rizariza69 Pas hari Kamis/Jumat berarti bakal dibahas lagi buat ngegabungin semuanya
Sekarang fokus untuk menyelesaikan bagian-bagian React dahulu:
Kalo udah bisa masing-masing, ngegabungin bakal lebih gampang
UI
Project React
React Folder Name
React Projects