1

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.

enter image description here


This is the script tags I used.

enter image description here

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?

9
  • 1
    do you provide the same redux store to both of your components? Was redux store changed after dispatching?
    – Oro
    Commented Dec 29, 2021 at 6:00
  • Yes. I am using the same redux store. Is it bad? Commented Dec 29, 2021 at 6:01
  • in the browser, redux extension shows my state is updated. So redux store is changing after dispatching. Commented Dec 29, 2021 at 6:03
  • 1
    Using the same redux store is correct. I meant both of your components have one parent with Provider and store there?
    – Oro
    Commented Dec 29, 2021 at 6:08
  • No. For those component I used two providers and importing the same store. 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. Commented Dec 29, 2021 at 6:10

0

Browse other questions tagged or ask your own question.