Redux 和React 如何一起使用
*把store.dispatch 方法傳遞給組件 ,內部可以調用修改狀態
*Subscribe 訂閱 render 函數 ,每次修改都重新渲染
*Redux相關文件 , 移動到 單獨文件 index.redux.js
index.js
import React from "react"
import ReactDom from "react-dom"
import { createStore } from "redux"
import App from "./App"
import { counter } from "./index.redux"
const store = createStore(counter)
function render (){
ReactDom.render(<App store = {store} />,document.getElementById("root"))
}
render ()
store.subscribe(render)
App.js
import React from "react"
import { ADDGUN } from "./index.redux"
import { Button } from "antd-mobile"
class App extends React.Component{
// constructor(props){
// super(props);
// }
render(){
const store = this.props.store
const num = store.getState()
return(
<div>
<h1>現在有機槍{num}把</h1>
<Button type="danger" onClick={()=>store.dispatch( ADDGUN())}>申請武器</Button>
</div>
)
}
}
export default App
index.redux.js
const ADD_GUN = "加機關槍"
const REMOVE_GUN = "減機關槍"
const FAKE_GUN = "搶機關槍"
/*reducer*/
export function counter (state=0,action){
switch(action.type){
case ADD_GUN:
return state + 1
case REMOVE_GUN:
return state - 1
case FAKE_GUN:
return state * 1000
default:
return 10
}
}
export function ADDGUN(){
return {type:ADD_GUN}
}
export function REMOVEGUN(){
return {type:REMOVE_GUN}
}
export function FAKEGUN(){
return {type:FAKE_GUN}
}