|
| 1 | +import React, { useReducer } from "react"; |
| 2 | + |
| 3 | +const MyContext1 = React.createContext({}); |
| 4 | +const MyContext2 = React.createContext({}); |
| 5 | + |
| 6 | +MyContext1.displayName = "MyContext1"; |
| 7 | +MyContext2.displayName = "MyContext2"; |
| 8 | + |
| 9 | + |
| 10 | +const ws = new WeakSet(); |
| 11 | +const foo = {}; |
| 12 | + |
| 13 | +ws.add(foo); |
| 14 | + |
| 15 | +let myMap = new Map() |
| 16 | +myMap.set("s", 'not a number') |
| 17 | + |
| 18 | +const valuesToTest = { |
| 19 | + f: new Set([1,2, 3,3]), |
| 20 | + g: ws, |
| 21 | + h: myMap, |
| 22 | + i: ws, |
| 23 | +} |
| 24 | + |
| 25 | +class Test2 extends React.Component { |
| 26 | + constructor(props) { |
| 27 | + super(props); |
| 28 | + |
| 29 | + this.state = { |
| 30 | + counter5: 0, |
| 31 | + }; |
| 32 | + } |
| 33 | + |
| 34 | + render() { |
| 35 | + const { counter5 } = this.state; |
| 36 | + const { id } = this.props; |
| 37 | + // const { id } = this.context; |
| 38 | + |
| 39 | + return ( |
| 40 | + <MyContext2.Provider value={{ id: counter5, b: "world", ...valuesToTest }}> |
| 41 | + <button onClick={() => this.setState({ counter5: counter5 + 1 })}> |
| 42 | + Click 5 Me {counter5} {id} |
| 43 | + </button> |
| 44 | + </MyContext2.Provider> |
| 45 | + ); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +Test2.contextType = MyContext1; |
| 50 | + |
| 51 | +function myReducer(state, action) { |
| 52 | + // counter++; |
| 53 | + // console.log(counter); |
| 54 | + switch (action.type) { |
| 55 | + case 'increment': |
| 56 | + return {count: state.count + 1}; |
| 57 | + case 'decrement': |
| 58 | + return {count: state.count - 1}; |
| 59 | + default: |
| 60 | + // throw new Error(); |
| 61 | + } |
| 62 | + |
| 63 | + return state; |
| 64 | +} |
| 65 | + |
| 66 | +const initialState = {count: 0}; |
| 67 | + |
| 68 | +function Counter() { |
| 69 | + const [state, dispatch] = useReducer(myReducer, initialState); |
| 70 | + // const dd = useContext(MyContext1); |
| 71 | + // const [state2, dispatch3] = useReducer(reducer, initialState); |
| 72 | + // const [abcd, setabcd] = useState(2); |
| 73 | + |
| 74 | + |
| 75 | + return ( |
| 76 | + <> |
| 77 | + {/* Count: {dd.id} */} |
| 78 | + Count: {state.count} |
| 79 | + <button onClick={() => dispatch({type: 'decrement'})}>-</button> |
| 80 | + <button onClick={() => dispatch({type: 'increment'})}>+</button> |
| 81 | + </> |
| 82 | + ); |
| 83 | +} |
| 84 | + |
| 85 | + |
| 86 | +export default function Home() { |
| 87 | + return ( |
| 88 | + <div> |
| 89 | + <Test2 /> |
| 90 | + <Counter /> |
| 91 | + </div> |
| 92 | + ) |
| 93 | +} |
0 commit comments