React
1import React from 'react' 2export default class ProductList extends React.Component { 3constructor(props) { 4 super(props) 5 this.state ={ 6 file: "", 7 imagePreviewUrl: "" 8 } 9 } 10 11handleFileChange = (id,e) => { 12 e.preventDefault() 13 const products = this.props.products; 14 let reader = new FileReader() 15 let file = e.target.files[0] 16 reader.onloadend = () => { 17 this.setState({ 18 file: file, 19 imagePreviewUrl: reader.result 20 }); 21 } 22 reader.readAsDataURL(file) 23 this.props.file(id, ??? )// ???の部分を色々試してみたがどれも期待値にならない 24// 試したこと reader,reader.result,this.state.imagePreviewUrlを???に入れてみた 25 26 } 27 28} 29 30render() { 31const product = this.props.products.map((product) => { 32 if (product.isVisible === true ){ 33 return( 34 <ul key={product.id}> 35 <li>{product.title}</li> 36 <li>{product.desc}</li> 37 <li>{product.price}</li> 38 <div> 39 <button type="submit" onClick={() => this.delete(product.id)}>削除</button> 40 <button type="submit" onClick = {(e) => this.handleButton(product.id , e)}>編集</button> 41 </div> 42 43 <div> 44 <input type="file" onChange={(e) => this.handleFileChange(product.id, e)}/> 45 <img src={this.state.imagePreviewUrl} /> 46 </div> 47 48 {product.editIsVisible && 49 <EditForm 50 product = {this.props.products} 51 edit = {this.edit} 52 id = {product.id} 53 />} 54 </ul> 55 ) 56 } 57 }); 58 const searchItem = this.props.products.filter( function( value ) { 59 return value.isVisible === true 60 }) 61 62 return ( 63 <div> 64 <h1>商品リスト</h1> 65 {product} 66 <h3>商品件数は{searchItem.length}件です</h3> 67 </div> 68 ) 69 }
React
1import React from 'react' 2 3export default class ProductContainer extends React.Component { 4 constructor(props) { 5 super(props) 6 this.state = { 7 products: [] 8 } 9 } 10 11add = (title, desc, price) => { 12 const newProducts = this.state.products 13 const product = { 14 id: newProducts.length + 1, 15 title: title, 16 desc: desc, 17 price: price, 18 fileName: "" } 19 newProducts.push(product) 20 this.setState({products: newProducts}) 21 } 22 23//fileName = reader.readAsDataURL(file)でbase64形式にエンコードされたURL 24file = (id,fileName) => { 25 const products = this.state.products; 26 //画像を表示したい配列を取得 27 const fileIndex = products.findIndex( product => product.id === id ) ; 28//(例)"data:image/jpeg;base64,/9j/... をfileNameに上書き保存したい 29 products[fileIndex].fileName = fileName 30 this.setState({products: products}) 31 } 32 33 34render() { 35 36 return ( 37 <div> 38 <ProductForm add={this.add} /> 39 <ProductList 40 products={this.state.products} 41 file = {this.file} 42 /> 43 </div> 44 45 46 ) 47 }
・現在の問題は下記のように一つの画像を変更すると全ての画像が変更されて表示されてしまうことです。
・期待値としては配列ごとに違う画像を表示させるようにしたいです。
足りない情報や、試してほしい情報は遠慮なくお申し付けください!
お力をいただきたいです。
imagePreviewUrl を products の各要素に持たせればいいと思いますが、ProductList と ProductContainer の関係がよく分かりません。
ご返信ありがとうございます。
ProductContainerに商品の一覧や、追加フォーム、編集フォームなどの機能を表示できるようなものにしています。
ProductListは商品の配列を一つずつに編集ボタンや削除ボタンなど、配列の中身をいじれるような機能にしています。
上記のコードにも少しだけ補足させていただきました!
自分もimagePreviewUrl を products の各要素に持たせれば表示できると考えているのですが、FileReaderのresultを取得できないのです。
コンソールではFileReaderのresultが ""と空白になっている部分とdate;image...となっている部分があるのですがなぜそのような値の中身の違いが発生しているのかも分からないです汗
回答1件
あなたの回答
tips
プレビュー