Professional Documents
Culture Documents
FREJMVORKU
Nemanja Zori
1.
O ReactJS
2.
Instalacija
Kako bi instalirali ReactJS bie nam potreban NodeJS kao i NPM, tj. Node
Package Manager. Potrebno je instalirati nekoliko paketa. Bie nam
potrebni neki babel pluginovi tako da emo prvo instalirati babel koristei
sledei kod:
C:\Users\username>npm install -g babel
C:\Users\username>npm install -g babel-cli
index.html
App.jsx
main.js
webpack.config.js
<head>
<meta charset = "UTF-8">
<title>React App</title>
</head>
<body>
<div id = "app"></div>
<script src = "index.js"></script>
</body>
</html>
JSX
React koristi JSX za ablone umjesto obinog JavaScript-a. Nije neophodno
koristiti ga, ali postoje odreeni razlozi za njegovo korienje:
var myStyle = {
fontSize: 100,
color: '#FF0000'
}
return (
<div>
<h1 style = {myStyle}>Header</h1>
</div>
);
}
}
return (
<div>
<h1>Header</h1>
{//End of the line Comment...}
{/*Multi line comment...*/}
</div>
);
}
}
3.
Komponente
this.state = {
data:
[
{
"id":1,
"name":"Foo",
"age":"20"
},
{
"id":2,
"name":"Bar",
"age":"30"
},
{
"id":3,
"name":"Baz",
"age":"40"
}
]
}
}
render() {
return (
<div>
<Header/>
<table>
<tbody>
{this.state.data.map((person, i) => <TableRow key = {i} data = {person}
/>)}
</tbody>
</table>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
3.2. Stanja
Stanje je mjesto odakle dolaze podaci. Praksa je da se uvijek pokua stanje
ostvariti to jednostavnije mogue i minimizirati broj takvih komponenti.
Ukoliko image, npr., deset komponenti kojima su potrebni podaci iz stanja,
trebali bi kreirati jednu kontejner komponentu koja e drati stanje za sve
njih:
import React from 'react';
this.state = {
header: "Header from state...",
"content": "Content from state..."
}
}
render() {
return (
<div>
<h1>{this.state.header}</h1>
<h2>{this.state.content}</h2>
</div>
);
}
}
A u main.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
a u main.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
App.defaultProps = {
headerProp: "Header from props...",
contentProp:"Content from props..."
}
a u main.js:
ReactDOM.render(<App/>, document.getElementById('app'));
this.state = {
header: "Header from props...",
"content": "Content from props..."
}
}
render() {
return (
<div>
<Header headerProp = {this.state.header}/>
<Content contentProp = {this.state.content}/>
</div>
);
}
}
a u main.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
ReactDOM.render(<App/>, document.getElementById('app'));
App.propTypes = {
propArray: React.PropTypes.array.isRequired,
propBool: React.PropTypes.bool.isRequired,
propFunc: React.PropTypes.func,
propNumber: React.PropTypes.number,
propString: React.PropTypes.string,
propObject: React.PropTypes.object
App.defaultProps = {
propArray: [1,2,3,4,5],
propBool: true,
propFunc: function(e){return e},
propNumber: 1,
propString: "String value...",
propObject: {
objectName1:"objectValue1",
objectName2: "objectValue2",
objectName3: "objectValue3"
}
}
a u main.js:
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App.jsx';
ReactDOM.render(<App/>, document.getElementById('app'));
this.state = {
data: []
}
this.setStateHandler = this.setStateHandler.bind(this);
};
setStateHandler() {
var item = "setState..."
var myArray = this.state.data;
myArray.push(item)
this.setState({data: myArray})
};
render() {
return (
<div>
<button onClick = {this.setStateHandler}>SET STATE</button>
<h4>State Array: {this.state.data}</h4>
</div>
);
}
}
Poeli smo sa praznim nizom. Svaki put kada kliknemo dugme, stanje se
aurira.
Ponekad elimo da komponentu auriramo runo. Ovo moemo postii
koristei forceUpdate() metod.
import React from 'react';
forceUpdateHandler() {
this.forceUpdate();
};
render() {
return (
<div>
<button onClick = {this.forceUpdateHandler}>FORCE UPDATE</button>
<h4>Random number: {Math.random()}</h4>
</div>
);
}
}
findDomNodeHandler() {
var myDiv = document.getElementById('myDiv');
ReactDOM.findDOMNode(myDiv).style.color = 'green';
}
render() {
return (
<div>
<button onClick = {this.findDomNodeHandler}>FIND DOME NODE</button>
<div id = "myDiv">NODE</div>
</div>
);
}
}
i na klijentskoj strani.
componentDidMount izvrava se prije prvog renderovanja samo na
klijentskoj strani. Ovdje se AJAX zahtjevi i DOM ili stanja auriraju ako
se dese. Ovaj metod se takoe koristi za integraciju sa drugim
JavaScript frejmvorcima i bilo kojim drugim sa odloenim
izvravanjem kao to su setTimeout ili setInterval. Koristimo ga da bi
aurirali stanje kako bi mogli da pokrenemo druge metode ivotnog
ciklusa.
componentWillReceiveProps se poziva im su props aurirani prije
nego to je bilo koji drugi render pozvan. Pozvan je iz
setNewNumber im je aurirano stanje.
constructor(props) {
super(props);
this.state = {
data: 0
}
this.setNewNumber = this.setNewNumber.bind(this)
};
setNewNumber() {
this.setState({data: this.state.data + 1})
}
render() {
return (
<div>
<button onClick = {this.setNewNumber}>INCREMENT</button>
<Content myNumber = {this.state.data}></Content>
</div>
);
}
}
componentWillMount() {
console.log('Component WILL MOUNT!')
}
componentDidMount() {
console.log('Component DID MOUNT!')
}
componentWillReceiveProps(newProps) {
console.log('Component WILL RECIEVE PROPS!')
}
shouldComponentUpdate(newProps, newState) {
return true;
}
componentWillUpdate(nextProps, nextState) {
console.log('Component WILL UPDATE!');
}
componentDidUpdate(prevProps, prevState) {
console.log('Component DID UPDATE!')
}
componentWillUnmount() {
console.log('Component WILL UNMOUNT!')
}
render() {
return (
<div>
<h3>{this.props.myNumber}</h3>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
setTimeout(() => {
ReactDOM.unmountComponentAtNode(document.getElementById('app'));}, 10000);
4.
Forme
constructor(props) {
super(props);
this.state = {
data: 'Initial data...'
}
this.updateState = this.updateState.bind(this);
};
updateState(e) {
this.setState({data: e.target.value});
}
render() {
return (
<div>
<input type = "text" value = {this.state.data}
onChange = {this.updateState} />
<h4>{this.state.data}</h4>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
constructor(props) {
super(props);
this.state = {
data: 'Initial data...'
}
this.updateState = this.updateState.bind(this);
};
updateState(e) {
this.setState({data: e.target.value});
}
render() {
return (
<div>
<Content myDataProp = {this.state.data}
updateStateProp = {this.updateState}></Content>
</div>
);
}
}
render() {
return (
<div>
<input type = "text" value = {this.props.myDataProp}
onChange = {this.props.updateStateProp} />
<h3>{this.props.myDataProp}</h3>
</div>
);
}
}
export default App;
ReactDOM.render(<App/>, document.getElementById('app'));
5.
Eventi
constructor(props) {
super(props);
this.state = {
data: 'Initial data...'
}
this.updateState = this.updateState.bind(this);
};
updateState() {
this.setState({data: 'Data updated...'})
}
render() {
return (
<div>
<button onClick = {this.updateState}>CLICK</button>
<h4>{this.state.data}</h4>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
constructor(props) {
super(props);
this.state = {
data: 'Initial data...'
}
this.updateState = this.updateState.bind(this);
};
updateState() {
this.setState({data: 'Data updated from the child component...'})
}
render() {
return (
<div>
<Content myDataProp = {this.state.data}
updateStateProp = {this.updateState}></Content>
</div>
);
}
}
render() {
return (
<div>
<button onClick = {this.props.updateStateProp}>CLICK</button>
<h3>{this.props.myDataProp}</h3>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
6.
ReactJS Refs
constructor(props) {
super(props);
this.state = {
data: ''
}
this.updateState = this.updateState.bind(this);
this.clearInput = this.clearInput.bind(this);
};
updateState(e) {
this.setState({data: e.target.value});
}
clearInput() {
this.setState({data: ''});
ReactDOM.findDOMNode(this.refs.myInput).focus();
}
render() {
return (
<div>
<input value = {this.state.data} onChange = {this.updateState}
ref = "myInput"></input>
<button onClick = {this.clearInput}>CLEAR</button>
<h4>{this.state.data}</h4>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
7.
ReactJS Keys
this.state = {
data:
[
{
component: 'First...',
id: 1
},
{
component: 'Second...',
id: 2
},
{
component: 'Third...',
id: 3
}
]
}
render() {
return (
<div>
<div>
{this.state.data.map((dynamicComponent, i) => <Content
key = {i} componentData = {dynamicComponent}/>)}
</div>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));
8.
ReactJS Ruter
{this.props.children}
</div>
)
}
}
<div>
<h1>Home...</h1>
</div>
)
}
}
ReactDOM.render((
<Router history = {browserHistory}>
<Route path = "/" component = {App}>
<IndexRoute component = {Home} />
<Route path = "home" component = {Home} />
<Route path = "about" component = {About} />
<Route path = "contact" component = {Contact} />
</Route>
</Router>
), document.getElementById('app'))
Sadraj
1.
O ReactJS.................................................................................................................1
2.
Instalacija................................................................................................................2
JSX..................................................................................................................................5
3.
Komponente............................................................................................................8
3.1.
Primjer komponente.........................................................................................9
3.2.
Stanja.............................................................................................................11
3.3.
Props (Rekviziti)..............................................................................................12
3.4.
Props Validacija...............................................................................................16
3.5.
Komponent API...............................................................................................18
3.6.
4.
Forme....................................................................................................................23
5.
Eventi....................................................................................................................26
6.
ReactJS Refs..........................................................................................................29
7.
ReactJS Keys.......................................................................................................30
8.
ReactJS Ruter......................................................................................................33