import { createStore } from 'redux' export const store = createStore(addTodo, 0); function addTodo(state = 0, { type, num = 1 }) { switch (type) { case "add": return state + num; default: return state; } return state; }
import logo from './logo.svg'; import './App.css'; import { store } from './redux/index' import { useState } from 'react'; const useStore = function (store){ const [stateValue, setStateValue] = useState(store.getState()) //变更订阅 store.subscribe(function(){ setStateValue(store.getState()); }) return [stateValue, setStateValue]; } export const Dom1 = function(){ const [s] = useStore(store); return <div> {s} </div> } function App() { const [s, ss] = useStore(store); const changeStore = function (){ store.dispatch({ type: 'add', num: 1 }); } return ( <div className="App"> <header className="App-header"> <img src={logo} className="App-logo" alt="logo" /> <p> Edit <code>src/App.js</code> and save to reload. </p> <p onClick={ changeStore }>{ s }</p> <Dom1></Dom1> </header> </div> ); } export default App;
标签:function,const,react,state,import,return,redux,createStore,store From: https://www.cnblogs.com/liao1992/p/17086833.html