我是React的新手,一切都很痛苦,尤其是理解專爲具有多年經驗的專業人員設計的文檔。如何在react-avatar-editor中使用函數
我的問題:反應頭像編輯器 https://github.com/mosch/react-avatar-editor 給出了可用「道具」的列表,這是我需要幫助的。我有一個用戶界面,但我不明白如何添加邏輯。我知道我不需要寫出所有我自己的功能。這就是使用這種包的目的。但我確實需要幫助理解簡潔的文檔。
我的代碼:沒有做任何讓人驚歎的事情,但它的構造方式是我認爲代碼可以工作,狀態設置,然後我需要函數,如handleScale()
,我不知道該怎麼寫但是,.setState並更改狀態。很明顯,我需要爲每個屬性提供其中的一個。
class MyEditor extends React.Component {
constructor(props){
super(props);
this.state = {
width: 250,
height: 250,
border: 50,
color: [255, 255, 255, 0.6],
scale: 1.2,
rotate: 0,
};
}
handleScale(scale){
this.setState({
scale:"some function that changes the scale"
})
}
render() {
return (
<div className="image-editor-container">
<AvatarEditor
image={this.props.url}
width={this.state.width}
height={this.state.height}
border={this.state.border}
color={this.state.color} // RGBA
scale={this.state.scale}
rotate={this.state.rotate}
/>
<div className="range-slider">Zoom:
<input type="range" onChange={this.state.handleScale}/>
</div>
</div>
)
}
}
但是文檔有一個Props列表和它們所做的列表。如何更改支柱的比例?這可能是一個荒謬的問題,但我試圖理解一些高於我的文檔並可以使用任何幫助。
我也可以使用幫助連接一個滑塊來改變圖像的大小。無論是國家還是道具 - 這是我認爲基本的JS,但仍然不確定。在React Dev Tools中,滑塊以道具的形式上下移動。 http://mosch.github.io/react-avatar-editor/docs/
在發佈之前,我到處搜索關於此主題的教程,在React中編輯圖像。如果有人知道任何,或任何教程重新:這個包,請直接在那裏!
那麼,文檔清楚地表明Scale是一個數字,所以在你的處理程序中它應該是:'handleScale(event){this.setState({scale:event.target.value}); }' –
既然它是一個輸入,我最近才知道它需要用'value'綁定到setState,我需要'event.target.value'。它在教程中看到了這個,並且忘記了。我不知道如何傳遞數字,但最終獲得滑塊來處理程序包的「縮放」功能。通過下面的這個和@DeividasKaržinauskas的解決方案,我能夠得到它的工作。一旦我確信自己知道我在說什麼,我就會解釋我是如何解決這個問題的。 –