Skip to content

Instantly share code, notes, and snippets.

@programarivm
Last active May 2, 2023 08:33
Show Gist options
  • Save programarivm/2c28b2fc1e8fad001f247bc7fa9a0d0f to your computer and use it in GitHub Desktop.
Save programarivm/2c28b2fc1e8fad001f247bc7fa9a0d0f to your computer and use it in GitHub Desktop.
Sharing state variables between React components
// src/features/dialog/PlayComputerDialog.js
import * as React from 'react';
import { useDispatch, useSelector } from 'react-redux';
import CloseIcon from '@mui/icons-material/Close';
import {
Button,
Dialog,
DialogContent,
DialogTitle,
Grid,
IconButton,
Slider,
Typography
} from '@mui/material';
import Pgn from '../../common/Pgn';
import Dispatcher from '../../common/Dispatcher';
import * as mainButtons from '../../features/mainButtonsSlice';
import * as mode from '../../features/modeSlice';
import * as playComputerDialog from '../../features/dialog/playComputerDialogSlice';
import SelectColorButtons from '../../features/dialog/SelectColorButtons';
import WsAction from '../../ws/WsAction';
const PlayComputerDialog = () => {
const state = useSelector(state => state);
const dispatch = useDispatch();
const [fields, setFields] = React.useState({
level: 1,
color: 'rand'
});
const handleCreateGame = () => {
// The fields are now available for sending to the server
// console.log(fields);
};
const handleLevelChange = (event: Event) => {
setFields({
...fields,
level: event.target.value
});
};
// ...
return (
<Dialog open={state.playComputerDialog.open} maxWidth="xs" fullWidth={true}>
<DialogTitle>
<Grid container>
<Grid item xs={11}>
Play Computer
</Grid>
<Grid item xs={1}>
<IconButton onClick={() => dispatch(playComputerDialog.close())}>
<CloseIcon />
</IconButton>
</Grid>
</Grid>
</DialogTitle>
<DialogContent>
<Typography
id="level"
gutterBottom
align="center"
>
Difficulty level
</Typography>
<Slider
name="level"
aria-label="Level"
defaultValue={1}
valueLabelDisplay="auto"
step={1}
min={0}
max={3}
marks
onChange={handleLevelChange}
/>
<Grid container justifyContent="center">
<SelectColorButtons props={fields} />
</Grid>
<Button
fullWidth
variant="outlined"
onClick={() => handleCreateGame()}
>
Create Game
</Button>
</DialogContent>
</Dialog>
);
}
export default PlayComputerDialog;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment