Her sınıf için tüm prop türleriimi açıkça belirtmeyi seviyorum.ReactJS: Nesneler için daha fazla modüler pervane çeşitleri ve şekilleri nasıl kullanabilirim?
React.createClass({
propTypes: {
optionalArray: React.PropTypes.array,
optionalBool: React.PropTypes.bool,
...
Bu yeniden kullanılabilir bileşenler okumaktan geçerli: https://facebook.github.io/react/docs/reusable-components.html
Ancak, ne ben birçok sınıflarında kullanmak çok yaygın bir nesne varsa? Örneğin: Burada
var MemoryForm = React.createClass({
propTypes: {
memory: React.PropTypes.shape({
memoryID: React.PropTypes.number,
content: React.PropTypes.string,
date: React.PropTypes.object,
dateStr: React.PropTypes.string,
note: React.PropTypes.string
}).isRequired,
...
var MemoriesArea = React.createClass({
propTypes: {
// The initial memory to fill the memory form with.
formMemory: React.PropTypes.shape({ // <== shape used again
memoryID: React.PropTypes.number,
content: React.PropTypes.string,
date: React.PropTypes.object,
dateStr: React.PropTypes.string,
note: React.PropTypes.string
}).isRequired,
// ...
var Playground = React.createClass({
getInitialState: function() {
var initVars = {
// The initial memory to fill the memory form with.
formMemory: { // <== shape is used again.
memoryID: 0,
content: "",
date: null,
dateStr: "",
note: ""
}
};
return initVars;
}
//...
, ben yanı sıra bazı başlatımlar olmak üzere çeşitli sınıflar için prop tiplerinde çok sık 'hafıza' şeklini kullanacak. Bunu nasıl daha fazla DRY yapabiliriz - yani daha az kod çoğaltma, bu nedenle bu nesne şeklindeki bir değişiklik gelecekte daha sürdürülebilir olacak mı?
Bu oldukça kaygan. Yine de ekleme/kaldırma işlemlerini nasıl kontrol ediyorsunuz? –
Benim endişem, eslint-plugin-react bir hata - https://github.com/yannickcr/eslint-plugin-react/issues/1389 gibi görünüyor. 'MemoryPropTypes.isRequired' öğesini ekleyebilir ancak eslint'i kapatmanız gerekir (en azından şimdilik). Bir önceki satırın üzerine '// eslint-disable-next-line react/no-typos 'bu yorumu ekleyin. Eğer 'memoryID' veya iç içe geçmiş herhangi bir PropType'ın istenmesini istiyorsanız 'lib/PropTypeValues.js' dosyasında yepyeni bir PropType oluşturmanız gerekiyor sanırım, değil mi? –