前提・実現したいこと
Next.js でlocalstorageをgetしたいです。
Listコンポーネントにて、localstorageから値を読み取り、表示することができません。(TypeError: Cannot read property 'Lists' of null)
localstorageに、記録することには成功しており以下の形式になっています。
MessageList:[{"Message_id":0,"FromUser":"name","DearUser":"name","Message":"text"}, [{"Message_id":2,"FromUser":"name","DearUser":"HI","Message":"This is for a text area."}]]
以下実装
index
1import Header from '../components/Header' 2import New_Message from '../components/New_Message'; 3import List from '../components/List'; 4export default () => 5 <div> 6 <Header> 7 8 </Header> 9 <New_Message initState="HI" /> 10 11 <List /> 12 13 </div>
NewMessage
1import React, { Component } from "react"; 2 3var mesid = 1; 4 5var MessageList = [{ 6 "Message_id": 0, 7 "FromUser": "name", 8 "DearUser": "name", 9 "Message": "text" 10}]; 11 12var UserList = { 13 "User_id": "0", 14 "UserName": "name", 15 "points": "0", 16 "applause":"0" 17} 18 19class New_Message extends Component { 20 21 constructor(props) { 22 23 super(props); 24 this.state = { 25 // 初期に表示するもの 26 usstate: props.initState, 27 desc: 'This is for a text area.', 28 }; 29 30 this.onSubmit = this.onSubmit.bind(this); 31 this.onTextAreaChange = this.onTextAreaChange.bind(this); 32 33 } 34 35 onChange(event) { 36 37 console.log(event.target.value); 38 this.setState({ usstate: event.target.value }); 39 40 } 41 42 onSubmit(event) { 43 44 mesid++; 45 46 var add_mesData = [{ 47 "Message_id": 0, 48 "FromUser": "name", 49 "DearUser": this.state.usstate, 50 // "DearUser": "name", 51 "Message": this.state.desc 52 }]; 53 54 add_mesData[0].Message_id = mesid; 55 MessageList.push(add_mesData); 56 console.log(JSON.stringify(MessageList, null, '\t')); 57 localStorage.setItem("MessageList", JSON.stringify(MessageList)); 58 59 event.preventDefault(); 60 console.log("onSubmit"); 61 console.log(this.state); 62 63 } 64 65 onTextAreaChange(e) { 66 67 this.setState({ desc: e.target.value }); 68 } 69 70 render() { 71 72 var states = [ 73 { code: "CA", name: "California" }, 74 { code: "HI", name: "Hawaii" }, 75 { code: "TX", name: "Texas"}, 76 { code: "WA", name: "Washington" } 77 ]; 78 79 var options = states.map( 80 (n)=>( 81 <option key={n.code} value={n.code}> 82 {n.name} 83 </option> 84 ) 85 ); 86 87 88 return ( 89 90 <form onSubmit={this.onSubmit}> 91 92 <div> 93 <select 94 value={this.state.usstate} 95 onChange={(event) => {this.onChange(event)} }> 96 {options} 97 </select> 98 </div> 99 100 <textarea 101 value={this.state.desc} 102 onChange={this.onTextAreaChange} /> 103 104 <div> 105 <button type="submit">OK</button> 106 </div> 107 108 </form> 109 110 ); 111 } 112} 113export default New_Message;
List
1import React, { Component } from "react"; 2import { loadGetInitialProps } from 'next/dist/next-server/lib/utils'; 3 4 5 6class List extends Component{ 7 8 componentDidUpdate() { 9 var MessageList = JSON.parse(localStorage.getItem('MessageList')); 10 console.log(MessageList); 11 this.state = { 12 Lists: MessageList 13 }; 14 } 15 16 17 render() { 18 19 return ( 20 <p> 21 {this.state.Lists} 22 </p> 23 ); 24 } 25 26} 27export default List;
発生している問題・エラーメッセージ
TypeError: Cannot read property 'Lists' of null List.render ./components/List.js:22 19 | render() { 20 | 21 | return ( > 22 | <p> | ^ 23 | {this.state.Lists} 24 | </p> 25 | ); View compiled
該当のソースコード
List
1import React, { Component } from "react"; 2import { loadGetInitialProps } from 'next/dist/next-server/lib/utils'; 3 4 5 6class List extends Component{ 7 8 componentDidUpdate() { 9 var MessageList = JSON.parse(localStorage.getItem('MessageList')); 10 console.log(MessageList); 11 this.state = { 12 Lists: MessageList 13 }; 14 } 15 16 17 render() { 18 19 return ( 20 <p> 21 {this.state.Lists} 22 </p> 23 ); 24 } 25 26} 27export default List;
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。