O
O
Ostic2019-06-13 12:35:06
React
Ostic, 2019-06-13 12:35:06

How to change context in React?

context.js

export ctx = {
  changed: false,
}

app.js
import ctx
import MyComponent
CTX = createContext(ctx)
<CTX.Provider value={ctx}>
    <MyComponent />
</CTX.Provider>

How to change value in context in MyComponent.js using
useContext ?
There is an example in the docs, but there on a "clean" context, without hooks.
Should I place a method on the context object?
Like so?
context.js
export ctx = {
  value: false,
  change: function(){
    this.value = !this.value
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-06-13
@Ostic

Change context from nested component

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question