React Context API - rerender component on context change without wrapping render in a consumer
I am using this.context to get context data inside the lifecycle methods, but when I am not wrapping component with Consumer, then when context data changes, my component is not getting rerendered.
So instead of this:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { itemsPerPage } = this.props;
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<SharedDataConsumer>
{({ companies }) => (
<React.Fragment>
<Companies
columns={columns}
data={companies}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
)}
</SharedDataConsumer>
);
}
I need to use something like this, in order to format that data before render:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { companies } = this.context;
const { itemsPerPage } = this.props;
const newComp = companies.map(value => ({
...value,
btn: this.actionBtns(value.id)
}));
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<React.Fragment>
<Companies
columns={columns}
data={newComp}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
);
}
But this way, component is not getting updated after context changes - context data is async.
I did research and saw this example where they wrap export and pass context data as props, but not sure if thats the correct way to go with latest context API?
Full code here.
reactjs react-context
add a comment |
I am using this.context to get context data inside the lifecycle methods, but when I am not wrapping component with Consumer, then when context data changes, my component is not getting rerendered.
So instead of this:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { itemsPerPage } = this.props;
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<SharedDataConsumer>
{({ companies }) => (
<React.Fragment>
<Companies
columns={columns}
data={companies}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
)}
</SharedDataConsumer>
);
}
I need to use something like this, in order to format that data before render:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { companies } = this.context;
const { itemsPerPage } = this.props;
const newComp = companies.map(value => ({
...value,
btn: this.actionBtns(value.id)
}));
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<React.Fragment>
<Companies
columns={columns}
data={newComp}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
);
}
But this way, component is not getting updated after context changes - context data is async.
I did research and saw this example where they wrap export and pass context data as props, but not sure if thats the correct way to go with latest context API?
Full code here.
reactjs react-context
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23
add a comment |
I am using this.context to get context data inside the lifecycle methods, but when I am not wrapping component with Consumer, then when context data changes, my component is not getting rerendered.
So instead of this:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { itemsPerPage } = this.props;
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<SharedDataConsumer>
{({ companies }) => (
<React.Fragment>
<Companies
columns={columns}
data={companies}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
)}
</SharedDataConsumer>
);
}
I need to use something like this, in order to format that data before render:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { companies } = this.context;
const { itemsPerPage } = this.props;
const newComp = companies.map(value => ({
...value,
btn: this.actionBtns(value.id)
}));
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<React.Fragment>
<Companies
columns={columns}
data={newComp}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
);
}
But this way, component is not getting updated after context changes - context data is async.
I did research and saw this example where they wrap export and pass context data as props, but not sure if thats the correct way to go with latest context API?
Full code here.
reactjs react-context
I am using this.context to get context data inside the lifecycle methods, but when I am not wrapping component with Consumer, then when context data changes, my component is not getting rerendered.
So instead of this:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { itemsPerPage } = this.props;
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<SharedDataConsumer>
{({ companies }) => (
<React.Fragment>
<Companies
columns={columns}
data={companies}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
)}
</SharedDataConsumer>
);
}
I need to use something like this, in order to format that data before render:
render() {
console.log('companies render');
const { modal, singleCompanyData, loading } = this.state;
const { companies } = this.context;
const { itemsPerPage } = this.props;
const newComp = companies.map(value => ({
...value,
btn: this.actionBtns(value.id)
}));
const columns = [
{ key: 'id', title: 'ID' },
{ key: 'title', title: 'Title' },
{ key: 'city', title: 'City' },
{ key: 'btn', title: 'Action' }
];
return (
<React.Fragment>
<Companies
columns={columns}
data={newComp}
addCompany={this.addCompany}
totalRecords={companies.length}
loading={loading}
itemsPerPage={itemsPerPage}
/>
<AM2Modal open={modal} handleModalClose={this.handleModalClose}>
<CompaniesEdit
singleCompanyData={singleCompanyData}
handleModalClose={this.handleModalClose}
inputChangeEvent={this.inputChangeEvent}
/>
</AM2Modal>
</React.Fragment>
);
}
But this way, component is not getting updated after context changes - context data is async.
I did research and saw this example where they wrap export and pass context data as props, but not sure if thats the correct way to go with latest context API?
Full code here.
reactjs react-context
reactjs react-context
edited Jan 1 at 20:38
Goran Jakovljevic
asked Jan 1 at 15:40
Goran JakovljevicGoran Jakovljevic
1,4911318
1,4911318
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23
add a comment |
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996752%2freact-context-api-rerender-component-on-context-change-without-wrapping-render%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996752%2freact-context-api-rerender-component-on-context-change-without-wrapping-render%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
what version of react do you use
– Shubham Khatri
Jan 4 at 10:37
I am using the latest version.
– Goran Jakovljevic
Jan 6 at 14:23