I have two widgets called "search widget" and "Name Widget", and I build those widget using webpack. I want to pass data from search widget to name widget. This is not like passing data from one component to another component. Below image shows that widgets.
This is the script tags I used.
action.js
export const addReminder = text => ({
type : "ADD_REMINDER",
payload : text
});
reducer.js
const initialState = {
reminders : []
};
const nameReducer = (state = initialState , action) => {
switch(action.type){
case 'ADD_REMINDER':
console.log("Name reducer action - ",action.payload);
return {
...state,
reminders: [...state.reminders, action.payload]
}
default:
return state;
}
}
export default nameReducer;
store.js
import nameReducer from '../reducers/nameReducer';
import { createStore} from 'redux';
const store = createStore(
nameReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
export default store;
search component code
import { addReminder } from '../Redux/actions/nameAction';
import { connect } from 'react-redux';
class searchComponent extends Component {
constructor() {
super();
this.state = {
name: '',
};
}
onChange(e) {
this.setState({
name: e.target.value,
});
}
onSubmit(e) {
e.preventDefault();
this.props.addReminder(this.state.name);
console.log("this is the form text", this.props.reminders);
}
render () {
return (
<div>
<form>
<input
type="text"
value={this.state.name}
onChange={e => this.onChange(e)} />
<br/><br/>
<button type="submit" onClick={e => {this.onSubmit(e)}}>Submit</button>
</form>
</div>
);
}
}
const mapStateToProps = (state) => ({
reminders: state.reminders
});
export default connect(mapStateToProps, {addReminder})(searchComponent);
name component code
import React, { Component } from 'react';
import { connect } from 'react-redux';
class nameComponent extends Component {
render() {
return (
<div>
<p>{this.props.reminders}</p>
</div>
)
}
}
const mapStateToProps = (state) => ({
reminders: state.reminders
});
export default connect(mapStateToProps)(nameComponent);
What I want is, when I enter something in search component and press submit button, the search value must display in the name component. But in the name component this.props.reminders didn't get any result. Do I need to pass variable in the script tag or How can I pass data from one widget to another widget?
ReactDOM.render( <Provider store={store}> <Search /> </Provider>, document.getElementById('widget:search'), );
this is the search widget one and for the name widget also I used this.