admin管理员组文章数量:1130349
I'm trying to use the RangeControl component. This is what I'm doing:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
}
}
),
this works Ok, but If I specify a different variable in the value parameter (and setup my attribute through the onChange event, instead), then the component stops working (actually, the onChange event is fired, but the component doesn't render correctly moving the current value indicator).
var my_variable = 1;
el(
RangeControl,
{
label: 'Columns',
value: my_variable,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
my_variable = val;
}
}
),
Is there something I'm missing here? Is this the intended behavior, or have I accidentally discovered a bug in the component?
Thank you!
I'm trying to use the RangeControl component. This is what I'm doing:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
}
}
),
this works Ok, but If I specify a different variable in the value parameter (and setup my attribute through the onChange event, instead), then the component stops working (actually, the onChange event is fired, but the component doesn't render correctly moving the current value indicator).
var my_variable = 1;
el(
RangeControl,
{
label: 'Columns',
value: my_variable,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
my_variable = val;
}
}
),
Is there something I'm missing here? Is this the intended behavior, or have I accidentally discovered a bug in the component?
Thank you!
Share Improve this question edited Jan 7, 2019 at 13:52 Andrea asked Jan 7, 2019 at 13:31 AndreaAndrea 235 bronze badges 2 |1 Answer
Reset to default 1When using a control that modifies a block attribute, you need to set the value of the control to the value of the attribute and the onChange function needs to change the attribute using the setAttributes function:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
props.setAttributes({ my_attribute: val })
}
}
),
The above example is linked to a block. The block attributes modify the component state. If you want to modify the state by your own, you could link it to the redux store. Or you could simply use the state of the component through the withState HOC. To do so you would need to wrap your component inside withState and use the props provided by it to update it.
const { withState } = wppose;
const { Component } = wp.element;
const { RangeControl } = wpponents;
class Range extends Component {
render() {
const { my_number, setState } = this.props;
return (
<RangeControl
value={my_number}
min={0}
max={3}
onChange={value => {
setState({ my_number: value });
}}
/>
);
}
}
export default withState({ my_number: 1 })(Range);
I'm trying to use the RangeControl component. This is what I'm doing:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
}
}
),
this works Ok, but If I specify a different variable in the value parameter (and setup my attribute through the onChange event, instead), then the component stops working (actually, the onChange event is fired, but the component doesn't render correctly moving the current value indicator).
var my_variable = 1;
el(
RangeControl,
{
label: 'Columns',
value: my_variable,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
my_variable = val;
}
}
),
Is there something I'm missing here? Is this the intended behavior, or have I accidentally discovered a bug in the component?
Thank you!
I'm trying to use the RangeControl component. This is what I'm doing:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
}
}
),
this works Ok, but If I specify a different variable in the value parameter (and setup my attribute through the onChange event, instead), then the component stops working (actually, the onChange event is fired, but the component doesn't render correctly moving the current value indicator).
var my_variable = 1;
el(
RangeControl,
{
label: 'Columns',
value: my_variable,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
my_variable = val;
}
}
),
Is there something I'm missing here? Is this the intended behavior, or have I accidentally discovered a bug in the component?
Thank you!
Share Improve this question edited Jan 7, 2019 at 13:52 Andrea asked Jan 7, 2019 at 13:31 AndreaAndrea 235 bronze badges 2-
What do you mean
If I specify a different variable in the value parameter? Could you share the code? – Alvaro Commented Jan 7, 2019 at 13:32 - @Alvaro, I've updated my original post – Andrea Commented Jan 7, 2019 at 13:38
1 Answer
Reset to default 1When using a control that modifies a block attribute, you need to set the value of the control to the value of the attribute and the onChange function needs to change the attribute using the setAttributes function:
el(
RangeControl,
{
label: 'Columns',
value: props.attributes.my_attribute,
initialPosition: 1,
min: 0,
max: 3,
onChange: function( val ) {
props.setAttributes({ my_attribute: val })
}
}
),
The above example is linked to a block. The block attributes modify the component state. If you want to modify the state by your own, you could link it to the redux store. Or you could simply use the state of the component through the withState HOC. To do so you would need to wrap your component inside withState and use the props provided by it to update it.
const { withState } = wppose;
const { Component } = wp.element;
const { RangeControl } = wpponents;
class Range extends Component {
render() {
const { my_number, setState } = this.props;
return (
<RangeControl
value={my_number}
min={0}
max={3}
onChange={value => {
setState({ my_number: value });
}}
/>
);
}
}
export default withState({ my_number: 1 })(Range);
本文标签: javascriptGutenberg RangeControl
版权声明:本文标题:javascript - Gutenberg RangeControl 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749035014a2306259.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


If I specify a different variable in the value parameter? Could you share the code? – Alvaro Commented Jan 7, 2019 at 13:32