score:1
instead of subscribing to the stores at your component level, why not subscribe where you render the components (e.g. menu.js)?
you could wrap your rendering code in a function, and in that function read the store's state before passing it as a prop to your component. you would call render()
manually to do your first render, but also subscribe to the store passing the render function as a callback:
// menu.js
var react = require('react');
var reactdom = require('react-dom');
var menu = require('./components/menu.react');
var commonstore = require('../stores/commonstore');
function render() {
var state = commonstore.getstate();
reactdom.render(
<menu data={state} />,
document.getelementbyid('menu')
);
}
commonstore.addchangelistener(render);
render();
note: i'm not sure what getters your store has, so i've just written some example code.
your component code would then simply be:
// ./components/menu.react.js
var react = require('react');
var commonactioncreators = require('../actions/commonactioncreators');
var menu = react.createclass({
_handleclickmenu: function() {
commonactioncreators.setmenuclieckon();
}
});
and then repeat the process for the info
component.
this fiddle shows an example of this approach working in practice (with some dummy components, store and action creators), to prove that the concept works: https://jsfiddle.net/vgskht45/1/
score:1
i think the main problem is you're not using your store to set the state in your components.
info.react.js should be:
var react = require('react');
var commonstore = require('../stores/commonstore');
var commonactioncreators = require('../actions/commonactioncreators');
var info = react.createclass({
componentdidmount: function() {
commonstore.addchangelistener(this._handlechangestore);
},
componentwillunmount: function() {
commonstore.remchangelistener(this._handlechangestore);
},
getinitialstate() {
return {
clickflag: commonstore.getclickflag()
};
},
_handlechangestore: function() {
this.setstate(clickflag: commonstore.getclickflag();
if (this.state.clickflag) {
console.log("menu is clicked"); // i want to detect the change.
}
},
});
additionally, you will want to have a condition that resets your clickflag.
update:
looking at your code, you shouldn't have each component using their own separate <script>
. instead you should have a central js (frequently called app.js) which then displays both the menu and info components. you then don't need to separate menu into js/menu.js and js/components/menu.react.js. likewise, you shouldn't have the info component separated into js/info.js and js/components/menu.react.js. you should just have js/components/menu.react.js and js/components/info.react.js, with app.js handling what you currently have js/menu.js and js/info.js doing.
example:
index.html
<div>
<div id="app"></div>
</div>
<script src="js/app.js"></script>
app.js
var react = require('react');
var reactdom = require('react-dom');
var menu = require('./components/menu.react');
var info = require('./components/info.react');
function renderdom() {
reactdom.render(
<app />,
document.getelementbyid('app')
);
}
renderdom();
info.react.js
var info = react.createclass({
componentdidmount: function() {
commonstore.addchangelistener(this._handlechangestore);
},
componentwillunmount: function() {
commonstore.remchangelistener(this._handlechangestore);
},
getinitialstate() {
return {
count: commonstore.getstate()
};
},
_handlechangestore: function() {
this.setstate(count: commonstore.getstate();
},
render: function() {
return (
<div>
info: {this.state.count}
</div>
);
},
});
Source: stackoverflow.com
Related Query
- ReactJS Flux: detect store data change between components (not parent and child relationship)
- Modal is not Closing ReactJS Hooks Using Parent and Child Components
- Data type does not correspnd between parent and child component in treact-typescript prop
- 2 way event-binding between parent and child components is not working
- how to Pass Data between Parent and Child Components
- Efficient way to pass data between parent and child element in reactjs [controlled vs uncontrolled components]
- Passing data child to parent functional components in reactjs
- Connecting child components to store vs connecting parent component to store and passing down props
- MapStateToProps and MapDispachToProps in parent and child components not working
- react stateless child components do not update on state change in parent component
- transition between components in reactjs not working and css properties not being applied
- Data from Parent to child using props in ReactJs is not working properly
- Parent - Child components and data responsibilities (should child call API?)
- React Router returns child components and not parent component
- what is the best practice for communication between child and parent components from the child end?
- ReactJS - Child components DO NOT render when imported component used, but DO when explicitly marked up in parent element
- Clearing state and input values between parent and child components in React
- How to share a data between child and parent component in React?
- unable to render lists of data while using class based component in both parent and child in reactjs
- this.props is not a function between parent and children components
- ReactJS: Maintain data state between parent and child
- What is the proper way to fetch data from server with ReactJS and Flux architecture where no Store presented?
- How to pass data from parent to child in reactJS after the components have rendered
- ReactJS - Maintaining common model between Parent and Child
- Pass state between Parent and Child components in React/Typescript
- reactjs useState and useEffect hooks not re-rendering after array data change for data received via websocket
- Data passing from parent component to the child component with reactjs and type script with the React.cloneElement
- passing data between child and parent component in nested component react js
- How to pass data between the function App() parent and the Material-UI nested sidebar child
- How to send array of objects data from child component to parent component and store in the parent object using react hooks?
More Query from same tag
- Vite React app: esbuild error in Docker container
- How to create costume tag using react jsx
- Loop through first 3 item in array JavaScript(react)
- Why useReducer does not update state in react-typescript
- React-Router 1.0.0-rc4 - dynamic router - params not working
- I'm getting this "Maximum update depth exceeded." while trying to handle firebase authentication error with useEffect
- ReactJS Add new row to Table
- programmatically navigate using react router problem
- Expected Corresponding JSX closing error
- Class constructor cannot be invoked without 'new' when returning <Component />
- How to update component based on container's state change
- How can I opt for React Styled-Components to generate a physical CSS file?
- How to get distinct values from an array of fetch API output
- Text paragraph with keywords tagged/highlighted by category
- Return exists but it still says, "Nothing was returned from render."
- Submit formsdata into a table in ReactJS
- webpack compilation error in the cypress test runner unexpected token
- How to change default colors in material ui
- Loop or map in React JS
- React NavBar Component Prevent render
- need to create a new hidden div that shows after a onclick
- React dynamic variable name with array index not working
- How to set mailto into a property of child react.js component
- Issue with MUI 5 DialogContent padding
- I have Some Error when add Button to my React Native App?
- How to I import an array (in js formating) from a json file?
- Redux props are being received in Redux DevTools but not in the React component
- passing js string literal with html into component and retain html
- Frame-Motion event handler doesn't work in child component with svg path
- How to make Styled components generic to receive parameters?