M
M
MSGroup2021-05-11 10:20:51
React
MSGroup, 2021-05-11 10:20:51

How to pass the state of a component to another non-child component?

Good afternoon.
There is a Datepicker.js file

const initialState = {
  startDate: null,
  endDate: null,
  focusedInput: null,
}

function reducer(state, action) {
  switch (action.type) {
    case 'focusChange':
      return {...state, focusedInput: action.payload}
    case 'dateChange':
      return action.payload
    default:
      throw new Error()
  }
}

function DateTimePicker1() {
  const [state, dispatch] = useReducer(reducer, initialState)
  console.log(state.endDate)


    return (
        <div>
            <DateRangeInput
              onDatesChange={data => dispatch({type: 'dateChange', payload: data})}
              onFocusChange={focusedInput => dispatch({type: 'focusChange', payload: focusedInput})}
              startDate={state.startDate} // Date or null
              endDate={state.endDate} // Date or null
              focusedInput={state.focusedInput} // START_DATE, END_DATE or null
            />


        </div>
  )
}

export default DateTimePicker1;


How to pass startDate={state.startDate}, endDate={state.endDate} values ​​to another JS "Table.js".
The goal is to filter the table by the range specified in the Datepicker.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2021-05-11
@miraage

https://reactjs.org/docs/context.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question